1

我正在与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);

这在我的三星 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
...

这是为什么?我该如何阻止这个?

4

0 回答 0