栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

不允许启动服务意图-Android Oreo

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不允许启动服务意图-Android Oreo

我也有同样的行为。

为什么会发生此问题?

由于Android 8
具有新的后台执行限制,因此您不应启动服务后台。

我如何解决

将您的迁移

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之后,此实施对我来说很有效,希望它对您有用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号