活动结束时服务停止

时间:2015-01-20 20:54:01

标签: java android android-intent android-activity android-service

我已经阅读了一些与这个问题相关的答案,它们似乎都是一样的:

“START_STICKY运行您的服务”
“在前台运行您的服务”
“使用startService运行您的服务,不要绑定它”

我正在做所有这些事情,我的服务STILL关闭并在每次关闭活动时重新启动。

这不是IntentService。 我也没有在onClick处理程序中调用stopSelf或stopService。

请向下滚动到我的更新 - 此行为已被确认为Android操作系统中的错误,我已将其报告给谷歌。 Click here to view the report.

从MainActivity启动我的服务:

svcIntent = new Intent(getBaseContext(), MyService.class);
startService(svcIntent);

在我的onStartCommand中:

        // Enter foreground state
    String title = "Service has been started...";
    String subject = "Service is running...";
    String body = "Monitoring your battery usage.";
    Notification notification = new Notification(R.drawable.theicon, title,
            System.currentTimeMillis());
    if (prefs.getBoolean("notificationSounds", true))
        notification.defaults |= Notification.DEFAULT_SOUND;
    else
        notification.sound = null;
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, subject, body, pendIntent);
    startForeground(1500, notification);

在我的onStartCommand结束时:

...
// Release WakeLock
wl.release();

    return START_STICKY;

更新

我想出了什么导致它!但我不知道如何解决它。在我的服务中,我还使用服务中的AlarmManager在指定的时间内设置对服务的函数调用。

        // Alarm manager setup for MyService
    AlarmManager AM = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    svcIntent1 = new Intent(this, AlarmReceiver.class);
    prefs.edit().putInt("initialBatt", initialBatt).apply();
    svcIntent1.setAction("com.myApp.servicealarm");
    pendingIntent = PendingIntent.getBroadcast(this, 93, svcIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set the alarm
    AM.set(AlarmManager.RTC_WAKEUP, timeNow + waitTime, pendingIntent);

我注意到如果我没有注释掉AM.set调用来设置闹钟,即使有一个EMPTY onReceive,当我在最近的应用程序中刷掉我的应用程序时,我的服务会在闹钟响起时被杀死。如果我注释掉设置的警报调用,那么在关闭我的应用程序后,服务永远不会被杀死并继续运行。有没有搞错?!我的算法功能需要这个警报!!

这很奇怪。一旦闹钟响起,我的调试消息就不会打印,我的服务会重新启动。但第二次,在服务重新启动后,调试消息会打印并且程序成功执行。

我已经尝试了这一点,它仍然会发生在普通的广播接收器上。我还将我的代码剥离到我的服务和广播接收器的设置警报调用,同样的事情发生,所以这不是我的算法。显然,如果您有一个设置闹钟的前台服务,当闹钟响起时,您的服务将重新启动。

结束

这种行为似乎是由Android操作系统中的错误引起的,所以我不希望得到答案。如果你想亲自看到这个bug,click here。我提供了一个项目,您可以编译并重现问题。

2 个答案:

答案 0 :(得分:4)

Android会在广播Intent发送(在您的应用中收到/处理之前)时终止此过程。

这是一个讨厌的Android错误,从4.4(API 19)开始。

请参阅https://code.google.com/p/android/issues/detail?id=63618&can=1&q=service%20restart%20broadcast&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

尤其是评论#22和#23

不幸的是,几乎所有"开放"问题被标记为"已过时"最近,假设它们都是在Android 5.0中修复的。开发人员无法重新打开过时的"问题。


编辑:添加有关前台广播的详细信息

根据链接问题中的信息,将Intent.FLAG_RECEIVER_FOREGROUND添加到广播Intent似乎可以确保在下次收到广播{{1}时不会杀死该进程}。

为此,请添加:

Intent

到您在svcIntent1.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

中设置闹钟的代码

请阅读链接问题中的评论以获取更多详细信息。

答案 1 :(得分:0)

尝试在单独的过程中运行您的服务。在你的清单中定义它:

<service
   android:name=".path.to.service.class"
   android:process=":my_service"/>
相关问题