JobService stopping by battery save mode and 15% - 10% - 5% status

45 Views Asked by At

I'm using a JobService to keep my notifications running even when app is closed by the user, but when cellphone goes to battery save mode or 15%, my service stop to work.

I would like to understand if exists a way to avoid it, thanks

TelaPrincipal.java


public class TelaPrincipal extends AppCompatActivity {

        cancelarJob();
        startarJob();

[...]

    }


    public void cancelarJob(){
        JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        scheduler.cancel(123);
        Log.d(TAG, "Job cancelled");
    }
    public void startarJob(){
        ComponentName componentName = new ComponentName(this, ExampleJobService.class);
        JobInfo info = new JobInfo.Builder(123, componentName)
                .setRequiresCharging(false)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setPersisted(true)
                .setPeriodic(15 * 60 * 1000)
                .build();

        JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d(TAG, "Job scheduled");
        } else {
            Log.d(TAG, "Job scheduling failed");
        }
    }
1

There are 1 best solutions below

1
On

You don't need any type of service of job to keep a notification around. Notifications stay until the user swipes them away. But no, there is no way to force this- when the battery is low, the OS kills everything it can to minimize power usage. Which is the right choice- the user will almost certainly prefer to miss your notification than have his phone die on him (although again, you don't need anything like this to keep a notification around)