Android job scheduler not persisted on reboot

2019-03-27 00:35发布

I have implemented Job scheduler in my project and it works fine in the case if the app is in background or if the app is killed. But it is not working if the device is rebooted. I have included

     JobInfo.Builder mBuilder = new JobInfo.Builder(TASK_ID, mComponentName);
     mBuilder.setPersisted(true);

in my builder and

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

in application manifest file.This is how I have added my service to manifest

     <service
        android:name="com.xxx.xxxx.service.MyJobService"
        android:permission="android.permission.BIND_JOB_SERVICE" />

Is there anything else to be included?

Thanks in advance

2条回答
手持菜刀,她持情操
2楼-- · 2019-03-27 00:48

Register a BroadCastReciever for detecting BOOT_COMPLETED.

<receiver android:name="com.example.startuptest.StartUpBootReceiver">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

And in your BroadcastReceiver:

public class StartUpBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
        ...
    }
  }
}

Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.

查看更多
疯言疯语
3楼-- · 2019-03-27 00:54

You need to call the setPersisted(boolean isPersisted) method.

You can find it in the doc https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setPersisted(boolean)

查看更多
登录 后发表回答