Created
August 9, 2019 15:19
-
-
Save RanjitPati/adbf9c6e098d7b70cabd02c51ad3c026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application { | |
private static final int JOB_ID = 3 * 9; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
if(!isJobServiceOn()){ | |
ComponentName mComponentName = new ComponentName(this, AnalyticsService.class); | |
//Now create a JobInfo and give | |
JobInfo mJobInfo = new JobInfo.Builder(JOB_ID, mComponentName) | |
.setPeriodic(15 * 60 * 1000).build(); | |
JobScheduler mScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); | |
int resultCode= mScheduler.schedule(mJobInfo); | |
if(resultCode == JobScheduler.RESULT_SUCCESS) | |
Log.e("JOB SCHEDULER SERVICE", "Job Scheduled"); | |
else | |
Log.e("JOB SCHEDULER SERVICE", "Job not Scheduled"); | |
} | |
} | |
@Override | |
public void onTerminate() { | |
super.onTerminate(); | |
JobScheduler mScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); | |
mScheduler.cancel(JOB_ID); | |
} | |
//This method will let us know whether the service is running or stopped | |
public boolean isJobServiceOn() { | |
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE ) ; | |
boolean hasBeenScheduled = false ; | |
for ( JobInfo jobInfo : scheduler.getAllPendingJobs() ) { | |
if ( jobInfo.getId() == JOB_ID ) { | |
hasBeenScheduled = true ; | |
break ; | |
} | |
} | |
return hasBeenScheduled ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment