1

I have used a timer to call a method at 5 seconds interval inside a job scheduler. But the job scheduler only running on app running stage,if i kill the app the job scheduler was not worked. How to run the job scheduler to work after i killing my app?

My Code is:

package com.deemsysinc.firebaseandroid;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.os.PersistableBundle;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class MyJobService extends JobService {
    private static final String TAG=MyJobService.class.getSimpleName();
    Timer timer;
    TimerTask timerTask;
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        final PersistableBundle persistableBundle=jobParameters.getExtras();
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                MethodCalling();
                Log.d("TheJobStart",persistableBundle.getString("Title"));
            }
        };
        timer.schedule(timerTask,0,15000);
        jobFinished(jobParameters,false);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.d("TheJobStart","JobCompleted");
        return true;
    }
}
Arigarasuthan
  • 315
  • 1
  • 8
  • 17

3 Answers3

1

Even if you implement WorkManager than also you will not be able to run Background task in Custom ROM where auto-start setting has been provided to the user like Redmi . For more info check this

Sujeet Kumar Mehta
  • 2,761
  • 2
  • 20
  • 28
  • 1
    I am also facing same issue. Is there anyone, who is able to resolve this issue? I have tried for disable no restriction on background data, no battery optimatization and enabled autostart feature in redmi. But job scheduler is not working after app killed by swiping in recent task list. – Manku Sep 07 '18 at 19:22
0

You have not provided any code. What and how you are doing, but instead of JobScheduler you should use WorkManager. It is recommended by Google as well. Even if your app is being killed process scheduled in WorkManager will be executed.

From official doc:

The WorkManager API makes it easy to specify deferrable, asynchronous tasks and when they should run. These APIs let you create a task and hand it off to WorkManager to run immediately or at an appropriate time. For example, an app might need to download new resources from the network from time to time. Using these classes, you can set up a task, choose appropriate circumstances for it to run (like "only while device is charging and online"), and hand it off to WorkManager to run when the conditions are met. The task is still guaranteed to run, even if your app is force-quit or the device is rebooted.

If your app is not running, WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager. You don't need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option.

You requirement comes under Recurring tasks, please check here for how to use WorkManager for recurring tasks.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • WorkManager is in alpha, i would recommend in this case to use FirebaseJobDispatcher. – Pablo Rodriguez Aug 20 '18 at 08:07
  • *Even if your app is being killed process scheduled in WorkManager will be executed.* -- same for jobscheduler. Which is not suprising since workmanager uses jobscheduler under the hood – Tim Aug 20 '18 at 08:10
  • @TimCastelijns I just mentioned Google's comment and it is **always not true** that WorkManager under the hood will always use JobScheduler. – Vikasdeep Singh Aug 20 '18 at 08:17
  • cannot resolve sympol worker error came any dependencies need to include? @ VicJordan – Arigarasuthan Aug 20 '18 at 10:14
  • @Arigarasuthan make sure that you are using compilesdkversion latest i.e. 28 – Vikasdeep Singh Aug 20 '18 at 10:34
  • iam using compile sdk version 27.i was added worked manager as dependency like implementation 'android.arch.work:work-runtime:1.0.0-alpha01' is it correct? – Arigarasuthan Aug 20 '18 at 10:57
  • @Arigarasuthan please add `implementation "android.arch.work:work-runtime:1.0.0-alpha02"` to `dependencies`. It should work. – Vikasdeep Singh Aug 20 '18 at 13:17
  • It is working on real device like redmi. Can someone able to solve the problem? – Manku Sep 10 '18 at 12:55
-1

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> I was facing the same problem but I found a solution just add these two lines in your manifest file .

RANAJEET BARIK
  • 185
  • 2
  • 7
  • 1
    com.google.android.c2dm.permission.RECEIVE is related with Google cloud messaging. It may not be related to JobScheduler. The above question didn't mentioned that it is using it anywhere. – binit92 Sep 06 '19 at 09:21