2016-03-31 31 views
6

我已经在我的Service上使用startforeground(),但Android在清除所有最近的应用程序时一直在阻止我的Service当我清除所有最近的应用程序时,Android系统杀死了我的服务

这里是我服务的代码:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    Notification notification = new NotificationCompat.Builder(this) 
      .setContentTitle("Text") 
      .setTicker("Text") 
      .setContentText("Text") 
      .setSmallIcon(R.drawable.icon) 
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)) 
      .build(); 

    startForeground(100, notification); 

    return START_STICKY; 
} 

有什么问题我与此代码做?

+0

您可以使用START_REDELIVER_INTENT而不是START_STICKY – rockstar

+0

抱歉,但我的服务在使用后仍然会死亡。 –

回答

12

长时间运行的服务需要以下使它少杀你服务可能被终止:

  1. 返回START_STICKYonStartCommand()。有了这个,即使必须停止资源限制,系统也会重新启动服务。

  2. 当服务未绑定到任何UI组件(例如Activity)时,该服务必须通过显示前台通知进入前台模式。前台服务不太可能被系统终止。

  3. 在清单文件的相应<service>标记中设置属性"stopWithTask"=false

另外,还要注意一些制造商的设备将终止服务,即使有上述特性,由于定制:

  1. 自定义任务管理器。
  2. 节电功能可以禁止非系统服务。

某些应用程序服务确实需要保持后台并且必须积极地保持活动状态。虽然不建议使用此方法,但可以执行以下步骤:

  1. 为服务启动添加触发器:如引导完成和网络连接广播。

  2. 如果服务在意图中接收意图/广播FLAG_RECEIVER_FOREGROUND

  3. 极端的手段是manually schedule a pending intent for service重新启动,AlarmManager,当onTaskRemoved()被调用。

此外,见:

  1. START_STICKY does not work on Android KitKat
+0

我使用了两个设备,其中一个是我自己定制的,结果发现另一个我认为没有定制的设备并没有杀死我的服务。我标记这个“接受”,因为这是杰克的答案更完整。谢谢。 –

+0

@zamronihamim用更多选项更新了答案。 –

+0

使用“onTaskRemoved()”查看了上次更新的链接后,我得到了它的工作。准确地说,从这个链接里面回答:http://stackoverflow.com/a/20681898/624109。你可以把上面的答案连接到你的更新,所以我们都可以立即看到它吗?谢谢。 –

1

可以使用START_REDELIVER_INTENT代替START_STICKY

这些最简单的解释是,

START_STICKY-告诉系统当有足够的内存来创建服务,全新副本,它从低内存恢复后。在这里,您将失去之前可能计算的结果。

START_NOT_STICKY-告诉系统即使在具有足够的内存时也不要打扰重启服务。

START_REDELIVER_INTENT-指示系统在崩溃后重新启动服务,并重新传送崩溃时出现的意图。

我希望这会帮助你。感谢

+0

对不起,但我的服务在使用 –

+0

后仍然死机,请更新所有活动代码和清单。 – rockstar

0

请仔细阅读本Click Here

一些设备具有定制的Android,Android系统并不真正的考验,当你明确的近期应用

+0

没错,我使用了两个设备,而我用来运行应用程序的其他设备并未杀死我的服务。谢谢您的回答。 –

+0

@zamronihamim是的,这确实是一些公司在Android的核心结构中发生变化,并且发生了这个问题。所以如果你想过来这个问题在你的应用程序的onDestroy方法发送广播在您的应用程序和注册广播接收器时务实地重新创建您的服务。如果您满意并且请接受它。 –

3

Android系统可以停止你的服务随时当他们想停下来保持设备性能或功耗。有很多情况下,Android系统可以停止您的服务,如低电量,应用程序长时间处于活动状态,设备处于睡眠模式,省电模式等。

我开发了一个技巧或黑客(可以说)通过使用,没有人可以停止您的服务(Android系统,第三方应用程序,用户)。

注意:通过使用此服务,您的服务将永不停止,并且可能会耗尽您的电池。

按照下面的步骤: -

1)返回START_STICKY在onStartCommand。

2)然后修改的onDestroy()和onTaskRemoved()在你的服务方法如下:

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show(); 


    Intent myIntent = new Intent(getApplicationContext(), YourService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.add(Calendar.SECOND, 10); 

    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

    Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show(); 

} 




@Override 
public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 


     Intent myIntent = new Intent(getApplicationContext(), YourService.class); 

     PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0); 

     AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); 

     Calendar calendar = Calendar.getInstance(); 

     calendar.setTimeInMillis(System.currentTimeMillis()); 

     calendar.add(Calendar.SECOND, 10); 

     alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

     Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show(); 


    } 




@Override 

    public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(getApplicationContext(), "Service Starts", Toast.LENGTH_SHORT).show(); 
     return START_STICKY; 
    } 

这里我每天会在服务停止时间(由Android系统或由用户设置报警手动),并使用PendindIntent每次在10秒内重新启动服务。

相关问题