我当前正在使用startWakefulService函数,该函数在Oreo中崩溃。我意识到我要么必须切换到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);
}
}
这是我在Android 8.x上运行时遇到的当前错误
致命异常:java.lang.RuntimeException 无法启动接收器com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver:java.lang.IllegalStateException:不允许启动服务意图{act = com.google.android.c2dm.intent.RECEIVE flg = 0x1000010 pkg = com.app.app cmp = com.app.app / .gcm.GCMIntentService(有其他功能)}:应用程序位于后台uid UidRecord {f602fba u0a114 RCVR空闲过程:1 seq(0,0,0)}
答案 0 :(得分:11)
我也有同样的行为。
为什么会发生此问题?
由于新版的Android 8 Background Execution Limits,您应该 无法启动服务背景。
我如何修复
将您的GCMIntetService
迁移到JobIntentService
而不是IntentService
。
请按照以下步骤操作: 1)将BIND_JOB_SERVICE权限添加到您的服务中:
<service android:name=".service.GCMIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>
2)在您的GCMIntentService
中,而不是扩展IntentService
,请使用android.support.v4.app.JobIntentService
并覆盖onHandleWork
然后从您的override
中删除onHandleIntent
。
public class GCMIntentService extends JobIntentService {
// Service unique ID
static final int SERVICE_JOB_ID = 50;
// Enqueuing work in to this service.
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GCMIntentService.class, SERVICE_JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
onHandleIntent(intent);
}
private void onHandleIntent(Intent intent) {
//Handling of notification goes here
}
}
最后,在您的GCMBroadcastReceiver
中,将您的GCMIntentService
放入队列。
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);
GCMIntentService.enqueueWork(context, (intent.setComponent(comp)));
}
}
在我们将目标sdk更新为27之后,该实现对我有用,我希望它对您有用。
答案 1 :(得分:1)
从Android O开始,后台检查限制使此类 一般更长的时间有用。 (通常不安全地开始服务 从收到广播后开始,因为您没有任何保证 此时您的应用处于前台状态,因此允许 这样做。)相反,开发人员应使用android.app.job.JobScheduler来 安排工作,这不需要应用程序唤醒 (系统将负责保持唤醒锁 工作)。
唯一的选择是启动ForeGroundService或使用JobScheduler / JobIntentService
您可以按以下步骤启动前台服务:
@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.
ContextCompat.startForegroundService(context,intent.setComponent(comp));
...
}
您还需要从“服务”类别中致电startService()
并显示通知。您可以按照我在SO上的回答来获取实施细节。