1

I have two applications I am building targeting Android 7.1 (running in emulator for this). App1 and App2

One of the apps contains a JobIntentService, which I have implemented overrides for onCreate and onhandleWork

This service is declared in App2's android manifest (com.example.app2) with

<service android:name:"MyJobIntentService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true" ></service>

From an activity in App1, I am attempting to launch the service with an intent:

Intent myServiceIntent = new Intent();
myServiceIntent.setComponent(new ComponentName("com.example.app2", "com.example.app2.MyJobIntentService"));
startService(myServiceIntent);

the problem is I get an exception

java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.app2/.MyJobIntentservice } without permission android.permission.BIND_JOB_SERVICE

Is this a bug? I am able to, for example remove the service definition from the manifest, and it will complain it can't find the service. I can also change export to false, and it will complain about that. It just doesn't seem to recognize the BIND_JOB_SERVICE permission.

Derek
  • 11,715
  • 32
  • 127
  • 228
  • You don't call `startService()` on a `JobIntentService` yourself. You use the static `enqueueWork()` method. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Service/JobIntentService). – CommonsWare Feb 07 '18 at 23:37
  • I actually saw this example earlier when I was looking around - but how does it work when I am trying to start a JobIntentService from another app? I tried to set it up from App1 with `JobIntentService.enqueueWork(this.getApplicationContext(), new ComponentName("com.example.app2", "com.example.app2.MyJobIntentService"), 1000, new Intent());` and I got the same SecurityException, just with a slightly different stacktrace – Derek Feb 08 '18 at 14:08
  • "but how does it work when I am trying to start a JobIntentService from another app?" -- you can't do that directly. `JobIntentService` is for internal use only. You would need something else (e.g., `BroadcastReceiver`) for the IPC to have App 1 tell App 2 "yo, it's time to do some work", and App 2 would then invoke the `JobIntentService` itself. – CommonsWare Feb 08 '18 at 14:33
  • Ah - I was under the impression you could use an explicit intent to launch a service, so I assumed this would apply to a JobIntentService as well, to let it run in the background. – Derek Feb 08 '18 at 14:40
  • 1
    Not the way that `JobIntentService` is written, in particular that `android:permission` attribute. That's one of the limitations of this particular workaround for Android 8.0's phase of the War on Background Processing. If the `BroadcastReceiver` solution is unworkable, you would need to roll back to the traditional `IntentService` approach, and have that service be a foreground service if it will run for an extended period of time. – CommonsWare Feb 08 '18 at 14:46

0 Answers0