Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JobScheduler - can't create a persistent job

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.

like image 977
Richard Lewin Avatar asked Apr 15 '15 15:04

Richard Lewin


2 Answers

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.

like image 60
Rajith Attapattu Avatar answered Oct 07 '22 15:10

Rajith Attapattu


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.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
like image 42
Cody Avatar answered Oct 07 '22 16:10

Cody