发生去电或来电时我正在开始工作
Bundle bundle = new Bundle();
bundle.putString("MobileNumber", number);
bundle.putString("CallStatus", "OUT");
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(ctx));
Job myJob = dispatcher.newJobBuilder()
.setService(FirebaseRecordingService.class) // the JobService that will be called
.setTag("my-unique-tag1") // uniquely identifies the job
.setExtras(bundle)
.build();
dispatcher.mustSchedule(myJob);
这次执行了onStartJob
现在通话结束后我要停止工作
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(ctx));
dispatcher.cancel("my-unique-tag1");
但是这次onStopJob没有执行。
谁能告诉我我做错了什么。
最佳答案
尝试此解决方案解决您的问题
当您开始作业时,只需使用此方法检查FirebaseRecordingService.Class
服务类是否正在运行
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
您的代码看起来像这样
if (isMyServiceRunning(context_here)) {
System.out.println("service already started stopping now");
stopService(new Intent(context_here, FirebaseRecordingService.class));
}
Bundle bundle = new Bundle();
bundle.putString("MobileNumber", number);
bundle.putString("CallStatus", "OUT");
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(ctx));
Job myJob = dispatcher.newJobBuilder()
.setService(FirebaseRecordingService.class) // the JobService that will be called
.setTag("my-unique-tag1") // uniquely identifies the job
.setExtras(bundle)
.build();
dispatcher.mustSchedule(myJob);
然后到执行工作时停止作为Firebase作业的服务
做这个
if (isMyServiceRunning(context_here)) {
System.out.println("service already started stopping now");
stopService(new Intent(context_here,FirebaseRecordingService.class));
}
而且不要这样
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(ctx));
它将创建FirebaseJobDispatcher类的另一个对象。
用于启动作业服务的声明对象
dispatcher.cancel("my-unique-tag1");
或者如果一次仅运行一项服务,您可以致电
dispatcher.cancelAll();
关于android - FirebaseJobDispatcher onStopJob不触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51646130/