Android JobIntentService ADB Error: Requires permi

2019-02-28 08:52发布

问题:

I'm trying to setup a JobIntentService and test it using adb.

When I use this command:

adb shell am startservice -a "com.example.package.action.DO_THING"

It produces the error:

Error: Requires permission android.permission.BIND_JOB_SERVICE

My AndroidManifest.xml has the following entry:

<service
    android:name=".services.intents.IntentHandlerService"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="true">
    <intent-filter>
         <action android:name="com.example.package.action.DO_THING"/>
    </intent-filter>
</service>

回答1:

JobIntentService, like JobService, is only usable from within an app. You cannot have another app talk to those services, nor is there any sort of direct PendingIntent option.

(in theory, you might be able to talk to a JobIntentService from other apps on Android 4.4 and older, as I'm not certain how android:permission="android.permission.BIND_JOB_SERVICE" will be handled when that permission is undefined, but I wouldn't count on it)

For IPC, you will need to use something else. So, for example, if you want a PendingIntent to be processed by a JobIntentService, create a broadcast PendingIntent with an explicit Intent pointing to a manifest-registered receiver, and have it post the job to the JobIntentService.