I am trying out the new JoScheduler API that has come with Android Lollipop. I have so far managed to successfully create and have a job run with a delay of 6000 milliseconds with no network requirements without an issue.
However, I have just tried to persist the same job via using the setPersisted(true) function. As soon as the job build() function is called it now fails saying that I need the RECEIVED_BOOT_COMPLETED permission in the Manifest file.
But I have added the permission:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.android" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I have even added the following code before the job is added to see if the app has the permission registered:
PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(Permission.RECEIVE_BOOT_COMPLETED,
context.getPackageName());
if (hasPerm == PackageManager.PERMISSION_GRANTED)
{
// Goes into here every time
}
However, when the job is built I get the following error:
java.lang.IllegalArgumentException: Error: requested job be persisted without holding RECEIVE_BOOT_COMPLETED permission.
My code to create and add the job to the JobSchedular:
ComponentName serviceComponent = new ComponentName(getApplicationContext(), MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(1, serviceComponent)
.setMinimumLatency(6000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true);
JobScheduler jobScheduler = (JobScheduler) getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
My JobService manifest declaration:
<service
android:name=".service.MyJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true" >
</service>
So I am wondering if I am doing something else wrong that anyone can spot. The only thing i'll note incase it does make a difference is that the code is run in an IntentService so I am wondering if this might be a reason why the JobScheduler can't find the permission.
I had the same issue, but then realized that I hadn't re-installed my app after adding the boot permission. So the app never got an opportunity to request the required permission when it was installed.
Once I uninstall and re-installed the app it works.
Note: My understanding is that Apps need to get their permission at install time. However I stand to be corrected here.
It looks like the JobScheduler keeps a cache of uids that hold this permission: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/android/server/job/JobSchedulerService.java#736
Perhaps you added the permission, but the phone didn't restart and therefore the JobScheduler has not updated its cache?
I had the same problem, I think your issue is that you are starting an intent service. You need to add the BIND_JOB permission to your intent service as well.
Just as 43matthew said, It's probably caused by JobScheduler cache.
It's solved after device reboot.
PS: Make sure you have the following permission in your XML.
I noticed that if job scheduler is set to persist then you may fall into this error, remove
from scheduler