提问者:小点点

隐式启动JobIntentService


如您所知,这就是我们如何激发JobintentService:

JobIntentService.enqueueWork(this,
                        MyJobIntentService.class,
                        JOB_ID, 
                        intent);

致命异常:java.lang.IllegalStateException:不允许启动服务意图{act=someaction pkg=somePackage(havs extras)}:app在后台uid UidRecord{de24eda u0a238 RCVR idle procs:1 seq(0,0,0)}

所以我转到JobintentService,现在的问题是我不能隐式启动它。
任何解决方案或替代方案都将受到欢迎。


共1个答案

匿名用户

我找到了一个解决方案,首先查询要查找的服务,并检查是否具有JobintentService的必要权限,然后排队工作:

private final String PERMISSION_BIND_JOB_SERVICE = "android.permission.BIND_JOB_SERVICE";
Intent handlerIntent = new Intent(MY_ACTION);
List<ResolveInfo> result =
        getPackageManager().queryIntentServices(handlerIntent, 0);
            if (result.size() > 0) {
    ResolveInfo info = result.get(0);
    if (info != null && info.serviceInfo != null &&
            info.serviceInfo.permission != null){
        String permission = info.serviceInfo.permission;
        if (permission.equals(PERMISSION_BIND_JOB_SERVICE)) {
            try {
                Class<?> serviceClass = Class.forName(info.serviceInfo.name);
                JobIntentService.enqueueWork(this,
                        serviceClass,
                        JOB_ID, handlerIntent);

            } catch (ClassNotFoundException e) {
                //Handle exception if you will
            }
        }
    }
}

在应用程序模块清单中,我还向我的服务添加了Android.permission.bind_job_service:

<service
    android:name=".MyService"
    android:enabled="true"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="false">
    <intent-filter>
        <action android:name="MY_ACTION" />
    </intent-filter>
</service>