Android JobScheduler - can't create a persiste

2020-06-08 20:55发布

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.

5条回答
Evening l夕情丶
2楼-- · 2020-06-08 21:26

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.

查看更多
女痞
3楼-- · 2020-06-08 21:34

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?

查看更多
【Aperson】
4楼-- · 2020-06-08 21:37

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.

    <service
        android:name=".your.intent.service"
        android:enabled="true"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"/>
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-06-08 21:44

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" />
查看更多
Emotional °昔
6楼-- · 2020-06-08 21:48

I noticed that if job scheduler is set to persist then you may fall into this error, remove

setPersist(true);

from scheduler

查看更多
登录 后发表回答