Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android job scheduler not persisted on reboot

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

like image 969
Pooja Rajendran C Avatar asked Feb 07 '17 07:02

Pooja Rajendran C


People also ask

Does JobScheduler persist even after reboot?

Given these conditions, the JobScheduler calculates the best time to schedule the execution of the job. Some examples of these parameters are the persistence of the job across reboots, the interval that the job should run at, whether or not the device is plugged in, or whether or not the device is idle.

What is the difference between JobScheduler and WorkManager in Android?

As mentioned in the previous section, WorkManager internally uses the JobScheduler API on Android 6.0 (API Level 23 – Marshmallow) and above devices. Therefore, there won't be any major differences between the WorkManager and JobScheduler on Android 6.0 and above devices.

How does JobScheduler work in Android?

JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory. So if you want to perform some background job like some network operations no matter your app is running or not.


1 Answers

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.

like image 74
karanatwal.github.io Avatar answered Oct 23 '22 20:10

karanatwal.github.io