FirebaseJobDispatcher onStopJob不会触发

时间:2018-08-02 05:29:59

标签: android

发生呼出或呼入电话时,我正在开始工作

 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没有执行。 谁能告诉我我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

尝试解决此问题的方法

开始作业时,只需使用此方法检查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();