编程语言
首页 > 编程语言> > java – Android:如何使用JobService的JobFinished

java – Android:如何使用JobService的JobFinished

作者:互联网

我没有看到使用jobService的JobFinshed的例子,似乎我们必须跟踪一些条件遇到的变化我们必须调用jobFinished(),我是对的吗?

解决方法:

从另一个类(如IntentService)调用jobFinished()的难度似乎是获取类的实例,该实例扩展了JobService以用于调用jobFinished().您可以获取有关预定作业的信息,但不能获取有关JobService的信息(至少,我找不到方法).我可以想到三种方法来调用jobFinished().

如果您不关心您的工作是否成功,或者您的工作花费的时间很少.

在我的一个JobService课程中,我正在做定期工作.我并不担心处理失败.任务将很快再次执行.如果是这种情况,您可以这样做.

    public boolean onStartJob(JobParameters params) {
        startService(new Intent(this, MyIntentServiceThatDoesTheWork.class));

        // job not really finished here but we assume success & prevent backoff procedures, wakelocking, etc.
        jobFinished(params, false);
        return true;
    }

如果您的工作足够短,那么这也是您想要的方式,在UI线程上执行它没有问题.在这种情况下,在onStartJob()中完成所有工作然后返回false.

使用BroadcastReceiver将消息从IntentService发送到JobService(每个类的单独文件).

    // common Strings
    public static final String IS_SUCCESS = "isSuccess";
    public static final String MY_BC_RCVR = "MyBroadcastRcvr";

你的JobService

    public class MyJobService extends JobService {
        JobParameters mParams;

        public boolean onStartJob(JobParameters params) {
            mParams = params;
            LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                    new IntentFilter(MY_BC_RCVR));
            startService(new Intent(this, MyIntentServiceThatDoesTheWork.class));
            return true;
        }

        private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                boolean isSuccess = false;
                if(intent.hasExtra(IS_SUCCESS)) {
                    isSuccess = intent.getBooleanExtra(IS_SUCCESS, false);
                }
                LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
                jobFinished(mParams, !isSuccess);
            }
        };
    }

&安培;你的IntentService

    public class MyIntentServiceThatDoesTheWork extends IntentService {
        @Override
        protected void onHandleIntent(Intent intent) {

            boolean isSuccess = methodToDoAllMyWork();
            Intent bcIntent = new Intent(MY_BC_RCVR);
            bcIntent.putExtra(IS_SUCCESS, isSuccess);
            LocalBroadcastManager.getInstance(this).sendBroadcast(bcIntent);
        }
    }

将您的工作线程类嵌套在JobService类中.

我已经从Google Developer Advocate给出了基于此Medium post(也由Arseny Levin引用)的AsyncTask示例,但也应该可以使用IntentService(请参阅此SO post以嵌套IntentService).

    public class MyJobService extends JobService {
        JobParameters mParams;

        public boolean onStartJob(JobParameters params) {
            mParams = params;
            new MyAsyncTaskThatDoesTheWork().execute();
            return true;
        }

        private class MyAsyncTaskThatDoesTheWork extends AsyncTask<Void, Void, Boolean> {
            @Override
            protected Boolean doInBackground(Void... params) {
                return methodToDoAllMyWork();
            }

            @Override
            protected void onPostExecute(Boolean isSuccess) {
                if(mParams != null) {
                    jobFinished(mParams, !isSuccess);
                }
            }
        }
    }

标签:android,java,android-jobscheduler,jobservice
来源: https://codeday.me/bug/20190724/1522914.html