本文介绍了Android:如何使用JobService的JobFinished的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到使用JobService的jobFinshed的示例,似乎我们必须在满足某些条件时跟踪更改,我们必须调用 jobFinished()方法,对吗?

I didn't see example of using jobFinshed of JobService, seems like we have to track changes when some condition meet we have to call jobFinished() method, am I right?

推荐答案

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

The difficulty of calling jobFinished() from another class like an IntentService seems to be getting an instance of your class that extends JobService to use to call jobFinished(). You can get info on the scheduled job, but not on the JobService (at least, I can't find a way). I can think of 3 ways to call jobFinished().

在我的 JobService 类之一中,我正在做定期工作.我不担心处理失败.该任务将尽快再次执行.如果是这样,您可以这样做.

In one of my JobService classes, I'm doing periodic work. I'm not worried about handling failures. The task will execute again soon enough. If this is the case, you can do this.

    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.

This is also the way you want to do it if your work is short enough it's no problem to do it on the UI thread. In this case, do all your work in onStartJob() then return false.

    // 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类中.

我已基于中级帖子(也由Arseny Levin引用),但也应该可以使用 IntentService (请参见 SO帖子code> IntentService ).

Nest your worker thread class in the JobService class.

I've given an example of an AsyncTask based on this Medium post (also referenced by Arseny Levin) from a Google Developer Advocate but it should also be possible to use an IntentService (see this SO post for nesting 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:如何使用JobService的JobFinished的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:24