gpt4 book ai didi

java - 不允许启动服务 Intent - Android Oreo

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:43 37 4
gpt4 key购买 nike

我目前正在使用在 Oreo 中崩溃的 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);
}
}

这是我在 Android 8.x 上运行时遇到的当前错误

Fatal Exception: java.lang.RuntimeException Unable to start receiver com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.app.app cmp=com.app.app/.gcm.GCMIntentService (has extras) }: app is in background uid UidRecord{f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}

最佳答案

我遇到了同样的行为。

为什么会出现这个问题?

Due to the new Background Execution Limits of Android 8 and you should not start services background.

我是如何修复它的

将您的 GCMIntetService 迁移到 JobIntentService 而不是 IntentService

请按照以下步骤操作:1) 为您的服务添加 BIND_JOB_SERVICE 权限:

<service android:name=".service.GCMIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>

2) 在您的 GCMIntentService 中,使用 android.support.v4.app.JobIntentService 并覆盖 onHandleWork 而不是扩展 IntentService然后删除 onHandleIntent 中的 override

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 后,这个实现对我有用,我希望它对你有用。

关于java - 不允许启动服务 Intent - Android Oreo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51353493/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com