在我们的应用程序中,有一个服务通常在Application.OnCreate
(直接调用context.startService
)期间启动,稍后通过AlarmManager
(正在进行重构以将其某些工作迁移到 JobScheduler)。
我们的应用程序也有一个BroadcastReceiver
以其直接意图启动的。
鉴于 Android Oreo ( https://developer.android.com/about/versions/oreo/android-8.0-changes.html ) 中的新限制,我们遇到了如下问题:
这会导致“IllegalStateException: Not allowed to start service Intent”崩溃。
我知道 CommonsWare 在此处/sf/answers/3115400361/所回答的启动服务的新推荐方法,但对于这种特定情况,我只想将if(process in foreground) { startService }
. 我目前正在使用以下方法,它似乎有效:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isProcessInForeground_V21(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = am.getAppTasks();
return tasks.size() > 0;
}
Run Code Online (Sandbox Code Playgroud)
但我找不到 Android Oreo 正在做的确切检查(我到这里为止https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/ContextImpl.startServiceCommon
方法上的java,但从那里requireForeground
标志似乎转到了一些本机实现)
所以我的问题: …
我目前正在使用在奥利奥崩溃的startWakefulService函数.我意识到要么必须切换到startForegroundService()并使用前台服务,要么切换到JobIntentService,但根据我的代码,我不知道该怎么做.(对不起,我是一个Android新手).任何正确方向的点都将非常感激.
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GCMIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在Android 8.x上运行时遇到的当前错误
致命异常:java.lang.RuntimeException无法启动接收器com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver:java.lang.IllegalStateException:不允许启动服务Intent {act = com.google.android.c2dm.intent.RECEIVE flg = 0x1000010 pkg = com.app.app cmp = com.app.app/.gcm.GCMIntentService(has extras)}:app在后台uid UidRecord {f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}
我正在构建一个音乐应用程序,一切正常,但最近,应用程序将崩溃。当在结构上看到我的崩溃列表时,请注意仅在 os 9 上发生。
致命异常:java.lang.RuntimeException 无法启动活动 ComponentInfo{android.store/android.store.MusicPlayerActivity}:java.lang.IllegalStateException:不允许启动服务 Intent { act=android.store.mediaservice.PLAY_PLAYLIST cmp=android .store/.mediaservice.MediaService (has extras) }: app is in background uid UidRecord{1aba0fa u0a192 SVC bg:+5m42s914ms idle change:uncached procs:1 seq(0,0,0)}
我无法重现该问题,因为它很少发生。以下代码导致崩溃:
if (intent.hasExtra(MediaService.EXTRAS_INDEX)) {
Intent i = new Intent(getApplicationContext(), MediaService.class);
i.setAction(MediaService.ACTION_PLAY_PLAYLIST); i.putExtra(MediaService.EXTRAS_INDEX, intent.getIntExtra(MediaService.EXTRAS_INDEX, 0));
i.putExtra(MediaService.EXTRAS_TRACK_IDLIST, intent.getStringArrayExtra(MediaService.EXTRAS_TRACK_IDLIST)); startService(i);
} else if (intent.hasExtra(EXTRAS_SHUFFLE)) {
Intent i = new Intent(getApplicationContext(), MediaService.class);
i.setAction(MediaService.ACTION_PLAY_SHUFFLE);
i.putExtra(MediaService.EXTRAS_TRACK_IDLIST, intent.getStringArrayExtra(MediaService.EXTRAS_TRACK_IDLIST));
startService(i);
}
Run Code Online (Sandbox Code Playgroud)
那么导致崩溃的主要原因是什么?对此的解决方案是什么?
随着Oreo中引入的新后台服务限制,我很难找到一个地方来澄清在后台运行应用程序时是否仍然可以从JobScheduler启动IntentServices。
我有一个基于地理围栏的非常重要的功能,该功能需要在篱笆断开后运行intentService。我花了很长时间设置它,以便在篱笆破损且应用程序处于“牛轧糖”的“打for”状态时排队等待工作。当重新打开网络连接窗口时,作业最终由操作系统运行时,我在intentService中一个接一个地剥离每个作业。从技术上来说,当应用程序不在前台时正在启动服务,或者由于使用JobScheduler而仍然允许这样做时,我现在可以不再执行此操作吗?
android android-intentservice android-jobscheduler android-8.0-oreo
I'm starting an IntentService from MainActivity:onCreate and I noticed this crash from the crash reporting only on Android Oreo above:
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.company.mobile/com.company.mobile.gcm.RegistrationIntentService }: app is in background uid UidRecord{c96fbae u0a184 TPSL idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon()(ContextImpl.java:1577)
at android.app.ContextImpl.startService()(ContextImpl.java:1532)
at android.content.ContextWrapper.startService()(ContextWrapper.java:664)
at cs.a()(FirebaseUtility.java:42)
at com.company.mobile.MainActivity.onCreate()(MainActivity.java:81)
at android.app.Activity.performCreate()(Activity.java:7136)
at android.app.Activity.performCreate()(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate()(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity()(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity()(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute()(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks()(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute()(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage()(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage()(Handler.java:106)
at android.os.Looper.loop()(Looper.java:193)
at android.app.ActivityThread.main()(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke()(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run()(RuntimeInit.java:493)
at …
Run Code Online (Sandbox Code Playgroud) android illegalstateexception android-intentservice android-8.0-oreo
Android Oreo 如何处理后台服务?
我创建了意图服务和服务背景,但由于 android Oreo 限制,当应用程序关闭时服务停止
WhatsApp 应用程序在 android 8.1 的后台运行,这怎么可能?
检查此图像:
此外,当应用程序关闭时广播接收器不工作