Rohit Mhatre
Rohit Mhatre

Reputation: 31

Job Scheduler and WorkManager are destroyed when the app is killed

Trying to put video compression in background but app gets killed WorkManager and JobSchedular Destroyed

how to solve this issue?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ComponentName componentName = new ComponentName(ConcatVideoActivity.this, ConcatVideoJobServiceRepository.class);
                    JobInfo jobInfo;
                    PersistableBundle bundle = new PersistableBundle();
                    bundle.putString(FIRST_IMG, doctorCredentialModel.getDoctorFrontimg().replaceAll(" ", "%20"));
                    bundle.putString(LAST_IMG, doctorCredentialModel.getDoctorBackimg().replaceAll(" ", "%20"));
                    bundle.putString(VIDEO_URL, doctorCredentialModel.getVidUrl());
                    if(doctorCredentialModel.getDoctorName()!=null){
                        bundle.putString(DR_NAME, doctorCredentialModel.getDoctorName());
                    }else{
                        bundle.putString(DR_NAME,getString(R.string.app_name));
                    }

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                        jobInfo = new JobInfo.Builder(12, componentName)
                                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                                .setPersisted(true)
                                .setExtras(bundle)
                                .build();
                    } else {
                        jobInfo = new JobInfo.Builder(12, componentName)
                                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                                .setPersisted(true)
                                .setExtras(bundle)
                                .build();
                    }
                    try {
                        JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
                        int resultCode = jobScheduler.schedule(jobInfo);
                        if (resultCode == JobScheduler.RESULT_SUCCESS) {
                            Log.d("JOB SECHEDULED", "Job scheduled!");

                        } else {
                            Log.d("JOB NOT SECHEDULED", "Job not scheduled");
                        }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }




@Override
public boolean onStartJob(JobParameters jobParameters) {
    Log.d(TAG, "Job started!");

    firstImg = jobParameters.getExtras().getString(FIRST_IMG);
    secondImg = jobParameters.getExtras().getString(LAST_IMG);
    videoUrl = jobParameters.getExtras().getString(VIDEO_URL);
    drName = jobParameters.getExtras().getString(DR_NAME);
    loadFFMpegBinary(jobParameters);
    isWorking = true;
    // We need 'jobParameters' so we can call 'jobFinished'
   // startConcatVideo(jobParameters); // Services do NOT run on a separate thread
    Toast.makeText(this, "Video Customization InProgress ", Toast.LENGTH_SHORT).show();
    return isWorking;
}

Upvotes: 2

Views: 3544

Answers (1)

pfmaggi
pfmaggi

Reputation: 6476

WorkManager uses JobScheduler when running on API Level 23+, for older OS releases it uses a combination of AlarmManager and BroadcastReceivers. JobScheduler on API Level 21-22 has some implementation issues on some devices and this is the reason that WorkManager starts to use JobScheduler only on Marshmallow.

So, for API Level 23+ WorkManager and JobScheduler behave in the same way. The advantage using WorkManager is that most of the features are available starting from API Level 14.

Regarding your issue, you don't specify on which device you see this issue. Have you tried to run your sample on the emulator with a stock android image to see if the issue is still present?

Another thing that may impact your background job is the 10 minutes limit that JobScheduler (and WorkManager) imposes to background work. After 10 minutes your job is stopped.

Some device manufacturers have decided to modify stock Android to force-stop the app. This is the the reason why WorkManager will stop working (as will JobScheduler, alarms, broadcast receivers, etc.). There is no way to work around this. Some device manufacturers do this, unfortunately, so in those cases WorkManager will stop working until the next time the app is launched.

This is reported on WorkManager's issuetracker.

Upvotes: 6

Related Questions