本文介绍了0流程和设置下,应用程序和运行1服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在一个活动开始startService服务我得到:

  1的流程和1个服务

如果我现在刷的活动了。即删除它,我得到:

  0进程和1个服务

这是为什么?什么是一个过程,是什么在Android世界服务?

我用START_STICKY,如果我停止通过设置,应用服务和运行,这不是又开始了,为什么?

UPDATE1一些code:

 活动:
startService(新意图(getApplicationContext(),MyService.class));服务:
@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    Log.d(TAG启动服务);
    返回(START_STICKY);
}


解决方案

原来是在奇巧的错误。
(有时候我觉得得到任何东西在Android中做的是一个大麻烦!)

Android服务:奇巧,当你刷卡的应用程序从最近用过远

服务将无法正常工作

在修复服务:

  @覆盖
公共无效onTaskRemoved(意向rootIntent){
    意图restartService =新意图(getApplicationContext(),this.getClass());
    restartService.setPackage(getPackageName());
    的PendingIntent restartServicePI = PendingIntent.getService(
        getApplicationContext(),1,restartService,
        PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。
    alarmService.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+1000,restartServicePI);
}

If I start a service with startService in a Activity I get:

1 processes and 1 service

If I now swipe that Activity away. I.e remove it, I get:

0 processes and 1 service

Why is this? And what is a Process and what is a Service in the Android world?

I use START_STICKY and if I stop the service via Settings, Apps and Running, it is not started again, why?

Update1 some code:

Activity:
startService(new Intent(getApplicationContext(), MyService.class));

Service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Starting service");


    return(START_STICKY);
}
解决方案

Turned out to be a bug in KitKat. (Sometimes I think getting anything done in Android is a big hassle!)

Android Services: KitKat, Service won't work when you swipe app away from recents

https://code.google.com/p/android/issues/detail?id=63793

Fix in Service:

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
        getApplicationContext(), 1, restartService,
        PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}

这篇关于0流程和设置下,应用程序和运行1服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:58