1

I am trying to test the reliability of using JobScheduler to schedule background tasks on Android API 23, because I noticed that sometimes it misbehaves and stops working.

This is how I schedule a job in SystemManager.java class:

public static void scheduleUploadJob(Context context) {
    ComponentName componentName = new ComponentName(context, BackgroundUploadJobScheduler.class);
    JobInfo info = new JobInfo.Builder(123, componentName)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setPersisted(true) 
        .setPeriodic(120*60*1000)  
        .build();

    JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
    int resultCode = scheduler.schedule(info);
    if (resultCode == JobScheduler.RESULT_SUCCESS) {
        Log.d("ScheduleJob()", "Job scheduled successfully!");
    } else {
        Log.d("ScheduleJob()", "Job scheduled Failed!");
    }
}

public class BackgroundUploadJobScheduler extends JobService {
    private static final String TAG = "JobSevice";
    public static boolean jobCancelled = false;

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "Events upload Job started");
        doBackgroundUploading(params, this);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "upload Job cancelled before completion");
        jobCancelled = true;
        return true;
    }

    public synchronized void doBackgroundUploading(final JobParameters params, final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (jobCancelled) {
                    return;
                }
                UploadObject uploadObject= //get it from RoomDB
                    NetworkManager.upload(context,uploadObject);
            }
            jobFinished(params, false);
        }
    }).start();
}

I do the network call in the NetworkManager.java class using Retrofit:

call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
        if (response.isSuccessful()) {
            SystemManager.scheduleEventRecordUploadJob(context);
        }else{
            SystemManager.scheduleEventRecordUploadJob(context);
        }
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        SystemManager.scheduleEventRecordUploadJob(context);
    }
});

The problem

After I initialized the app and made the first Network call, I exited the app, disconnected the USB connection and turned off the screen. I left it overnight and came to check the records on my server DB for results.

  1. The device kept sending the server network data with almost the same rate at least twice per minute for a continuous 10 hours! (This wasn't expected, cus the device should enter doze right?)
  2. After that there was no more requests sent! From 3 AM till 9:30 AM I got nothing sent to the server. I turned on my device screen, connected it to power, opened the app... waited for an hour, still nothing more is being sent. Why? Did the service just get killed? How can I solve such an issue?

N.B

Of course, I do not intend to do such heavy repeating work like this on a real application. But, I am just doing a test app to understand the behavior of JobScheduler and JobService. And why does it just die out of nothing all of a sudden and doesn't start again?!

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Mena
  • 3,019
  • 1
  • 25
  • 54
  • Which device are you using to test this? Certain OEMs are infamous for killing background processes. – Darshan Pania Aug 27 '19 at 09:38
  • @DarshanPania Samsung Galaxy Tab A 9.7 & S Pen (SM-P555), Android API 23. https://www.gsmarena.com/samsung_galaxy_tab_a_9_7_&_s_pen-7443.php – Mena Aug 27 '19 at 09:43
  • I have had this problem with a well known app at some point , go check your device settings and check if background activities are not limited in the device itself, use an emulator to test ..... here is an article to help https://www.techrepublic.com/article/how-to-limit-app-background-activity-in-android-oreo/ just do the opposite – Ruben Meiring Aug 27 '19 at 12:14

0 Answers0