小米Android 7:JobIntentService.onHandleWork循环运行

问题描述

我正在与JobIntentService类一起做一些背景工作:


public class JsInvokerJobService extends JobIntentService {
    /**
     * Unique job ID for this service.
     */
    static final int JOB_ID = 1000;
    private static final String CHANNEL_ID = "AppsAndJunk";
    private static final String TAG = "JsInvokerJobService";


    /**
     * Convenience method for enqueuing work in to this service.
     */
    static void enqueueWork(Context context,Intent work) {
        enqueueWork(context,JsInvokerJobService.class,JOB_ID,work);
    }

    private void createNotificationChannel() {
        ...
    }

    final Handler mHandler = new Handler();

    @Override
    protected void onHandleWork(final Intent intent) {
        createNotificationChannel();
        Log.i(TAG,"Executing work: " + intent);

        mHandler.post(new Runnable() {
            @Override public void run() {
                Log.i(TAG,"Inside runnable " + intent);
                JsInvokerJobService.this.startService(intent);
            }
        });

        Log.i(TAG,"Finished work: " + intent);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"All work complete");
    }


}

这是该类的工作入队方式:

Intent serviceIntent = new Intent(context,JsInvokerService.class);
serviceIntent.putExtra("intentAction",intentAction);
serviceIntent.putExtra("intentData",intentData);
if(originalIntentExtras != null) {
    serviceIntent.putExtras(originalIntentExtras);
}
JsInvokerJobService.enqueueWork(context,serviceIntent);

这在我的Samsung Android 9上完美运行。在日志中,我看到了每项工作:

09-30 11:31:03.599 10650 10924 I JsInvokerJobService: Executing work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.599 10650 10924 I JsInvokerJobService: Finished work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.599 10650 10650 I JsInvokerJobService: Inside runnable Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.607 10650 10650 I JsInvokerJobService: All work complete

但是当我在小米Android 7上进行测试时,我看到此onHandleWork方法被无限期调用

09-30 11:31:03.599 10650 10924 I JsInvokerJobService: Executing work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.599 10650 10924 I JsInvokerJobService: Finished work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.599 10650 10650 I JsInvokerJobService: Inside runnable Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.607 10650 10650 I JsInvokerJobService: All work complete
09-30 11:31:03.610 10650 10700 I JsInvokerJobService: Executing work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.611 10650 10700 I JsInvokerJobService: Finished work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.611 10650 10650 I JsInvokerJobService: Inside runnable Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.619 10650 10650 I JsInvokerJobService: All work complete
09-30 11:31:03.623 10650 10784 I JsInvokerJobService: Executing work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.623 10650 10784 I JsInvokerJobService: Finished work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.623 10650 10650 I JsInvokerJobService: Inside runnable Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.630 10650 10650 I JsInvokerJobService: All work complete
09-30 11:31:03.633 10650 10703 I JsInvokerJobService: Executing work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.633 10650 10703 I JsInvokerJobService: Finished work: Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.634 10650 10650 I JsInvokerJobService: Inside runnable Intent { cmp=com.smartsecurityxzt/.JsInvokerJobService (has extras) }
09-30 11:31:03.639 10650 10650 I JsInvokerJobService: All work complete
...

那是为什么?如何停止呢?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)