Jay*_*ayB 5 java android android-intent android-service android-activity
我已经阅读了一些与这个问题相关的答案,它们似乎都是一样的:
"使用START_STICKY运行您的服务" 
"在前台
运行您的服务" "使用startService运行您的服务,不要绑定它"
我正在做所有这些事情,我的服务STILL关闭并在每次关闭活动时重新启动.
这不是IntentService.我也没有在onClick处理程序中调用stopSelf或stopService.
请向下滚动到我的更新 - 此行为已被确认为Android操作系统中的错误,我已将其报告给谷歌. 点击此处查看报告.
从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操作系统中的一个错误引起的,所以我不希望得到答案.如果您想亲自看到此错误,请单击此处.我提供了一个项目,您可以编译并重现问题.
IntentAndroid 在发送广播时(在您的应用程序中接收/处理广播之前)终止该进程。
从 4.4 (API 19) 开始,这是一个严重的 Android 错误。
特别是评论 #22 和 #23
不幸的是,几乎所有“开放”问题最近都被标记为“过时”,并假设它们都已在 Android 5.0 中得到修复。开发人员无法重新打开“过时”的问题。
编辑:添加有关前台广播的详细信息
Intent.FLAG_RECEIVER_FOREGROUND根据链接问题中的信息,添加到广播中似乎Intent可以确保该进程不会在下次收到广播时被终止Intent。
为此,请添加:
svcIntent1.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
到您在AlarmManager.
请阅读链接问题中的评论以了解更多详细信息。
| 归档时间: | 
 | 
| 查看次数: | 1729 次 | 
| 最近记录: |