java 如何在android中检查JobService是否正在运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46543658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check JobService is running or not in android?
提问by New Developer
I am using JobService in my project. It's working good. But sometimes service stopped. It is not restart again. So that I am trying to strat the JobService if not running. But I don't know how to check JobService is already running or not. Please let me any idea to how to check JobService is running or not.
我在我的项目中使用 JobService。它运作良好。但有时服务停止。它不是再次重启。因此,如果没有运行,我会尝试对 JobService 进行分层。但我不知道如何检查 JobService 是否已经在运行。请让我知道如何检查 JobService 是否正在运行。
My Job service class:
我的工作服务类:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class JobService extends android.app.job.JobService {
@Override
public boolean onStartJob(JobParameters params) {
if (NetworkHandler.getInstance().isNetworkAvailable()) {
TaskHandler.getInstance().getTaskListFromServer(PreferenceManagerForPlannedRoute.getInstance().getLastSyncTime(), PreferenceManagerForPlannedRoute.getInstance().getInProgressMoveTask());
NotificationUtils.getInstance().CreateNotification();
Logger.d("Scheduler", "Working!!");
} else {
Logger.d("Network", "not available");
}
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
Logger.d("onStopJob", "Stopped");
return true;
}
}
My Util Class:
我的实用类:
public class JobSchedulerUtils {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void scheduleJob(Context context) {
ComponentName serviceComponent = new ComponentName(context, JobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
builder.setRequiresDeviceIdle(false);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPeriodic(5000);
builder.setPersisted(true);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
}
}
To start:
开始:
synchronized public void startAlarmManager() {
if(Build.VERSION.SDK_INT>=21) {
JobSchedulerUtils.scheduleJob(Application.getInstance().getApplicationContext());
}else {
Intent myIntent = new Intent(Application.getInstance(), AlarmManagerForPlannedRoute.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Application.getInstance(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) Application.getInstance().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), Constants.TIME_DELAY_FOR_TASK_LIST_SERVICE_CALL_IN_SECONDS * 1000, pendingIntent);
}
}
To Stop:
停止:
synchronized private void stopAlarmManager(Activity activity) {
if(Build.VERSION.SDK_INT>=21){
JobScheduler jobScheduler = (JobScheduler) Application.getInstance().getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancelAll();
}else {
Intent myIntent = new Intent(activity, AlarmManagerForPlannedRoute.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
}
回答by New Developer
I got the solution.
我得到了解决方案。
public static boolean isJobServiceOn( Context context ) {
JobScheduler scheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE ) ;
boolean hasBeenScheduled = false ;
for ( JobInfo jobInfo : scheduler.getAllPendingJobs() ) {
if ( jobInfo.getId() == JOB_ID ) {
hasBeenScheduled = true ;
break ;
}
}
return hasBeenScheduled ;
}
Using of this method we can check jobService is running or not.
使用这个方法我们可以检查 jobService 是否正在运行。