Android服务:从电话(全部清除)任务中重启一次或从任务堆栈中删除

时间:2017-08-10 11:51:42

标签: android android-activity android-service background-process android-service-binding

我正在尝试在后台持续运行我的服务。我知道它会耗费大量电池,但它仍然是我的一个用例。

案例1:使用startService(intent)方法启动BackgroundService。 案例2 :使用bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

启动BoundService

在这两种情况下,我的服务onStartCommand代码如下所示,

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(count < 10000){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;

            }

        }
    }).start();
    // TODO: flag not start itself when stopped
    // TODO: use START_STICKY to keep running
    return START_STICKY;
}

当我的服务没有死的情况意味着它将继续运行:

案例1:如果我按HOME按钮。 案例2:如果我按下并退出应用程序。

当我的服务肯定会被杀死时的场景:

案例1:如果我从手机的任务堆栈中删除我的应用程序。 案例2 :我转到设置并停止服务。

我希望我的服务在上述任何一种情况下都会被杀死。

我已经从stackoverflow中提到了很多问题,这样做,

  @Override
    public void onDestroy() {
        super.onDestroy();

        startService(new Intent(this, BackgroundService.class));
    }

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

        startService(new Intent(this, BackgroundService.class));

    }

但绝对onDestroy()不是那种每次都会被调用的方法。我在日志中查了一下。

2 个答案:

答案 0 :(得分:3)

如果用户执行了强制停止&#34;在你的应用上,你的Service将被杀死,你无能为力阻止它。您也无法自动重新启动Service,因为用户已告知Android它不再希望您的应用运行。用户必须手动重新启动您的应用。

如果您从最近的任务列表中移除您的应用程序&#34;,Android将终止托管您应用的操作系统进程。假设您的Service也在此OS进程中运行,它也将被终止。在这种情况下,假设您已从START_STICKY返回onStartCommand(),Android将在新的操作系统进程中重新启动Service

某些设备(小米,华为,其他一些中国手机,一些LG和联想设备)必须将您的应用手动添加到受保护的应用列表中。或者&#34;允许在后台运行的应用程序&#34;以便Android自动重启Service。在这些设备上,即使您从Service返回START_STICKY,Android也不会自动重新启动onStartCommand()

另见Situations when a Service's onDestroy() method doesn't get called?

答案 1 :(得分:0)

我使用以下代码段使用RTC重复警报

 Context ctx = getApplicationContext();
        Calendar cal = Calendar.getInstance();
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        long interval = 100 * 5;
        Intent serviceIntent = new Intent(ctx, BackgroundService.class);
        PendingIntent servicePendingIntent =
                PendingIntent.getService(ctx,
                        BackgroundService.SERVICE_ID,
                        serviceIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT); 
        am.setRepeating(
                AlarmManager.RTC,
                cal.getTimeInMillis(),
                interval,
                servicePendingIntent
        );

使用PendingIntent.FLAG_UPDATE_CURRENT,它正在更新当前服务本身还有其他标志,这将取消现有服务并开始一个新服务。

BackgroundService.SERVICE_ID,在服务中创建了静态。

这位Android开发者官方文档帮助了我,

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application.

https://developer.android.com/training/scheduling/alarms.html#set