Android Priority Job Queue使用(1)

1. 简介 
Android Priority Job Queue是一款专门为Android编写,实现了Job Queue的后台任务队列按优先级处理
github地址:https://github.com/path/android-priority-jobqueue ,直接下载使用,貌似demo不能正常运行,参考其他资料自己写了一个demo: https://github.com/HungryGoogle/LeeArchtecture.git


2. 背景 
几乎所有的应用程序都存在后台线程工作。这些“背景任务”需要保持应用程序响应性和鲁棒性,特别是在不利的情况下(如有限的网络连接)。在安卓应用中,有几种方法来实现后台工作: 
1) 异步任务: 
2) 使用service: 
3) 本文讲的Android Priority Job Queue 作业队列提供了一个很好的框架来完成上述所有的工作。当确定了你的后台任务的工作时,将它们作为Job添加到你jobmanager实例。Job Manage会照顾优先级,持久性,负载平衡,延迟,网络控制,分组等,它还提供了一个很好的生命周期,为工作提供一个更好的,一致的用户体验。 


3. 使用(基于android studio) 
1.添加依赖

compile 'com.birbit:android-priority-jobqueue:1.3.5'
 
 
  • 1

2.创建Application并配置JobManager

public class AppApplication extends Application
{
    private JobManager jobManager;
    public static AppApplication instance;
    @Override
    public void onCreate() {
        super.onCreate();
        configureJobManager();
    }

    public AppApplication(){
        instance=this;
    }
    public JobManager getJobManager() {
        return jobManager;
    }

    public static AppApplication getInstance() {
        return instance;
    }

    private void configureJobManager() {
        Configuration configuration = new Configuration.Builder(this)
                .customLogger(new CustomLogger() {
                    private static final String TAG = "JOBS";
                    @Override
                    public boolean isDebugEnabled() {
                        return true;
                    }

                    @Override
                    public void d(String text, Object... args) {
                        Log.i(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(Throwable t, String text, Object... args) {
                        Log.e(TAG, String.format(text, args), t);
                    }

                    @Override
                    public void e(String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }
                })
                .minConsumerCount(1)//always keep at least one consumer alive
                .maxConsumerCount(3)//up to 3 consumers at a time
                .loadFactor(3)//3 jobs per consumer
                .consumerKeepAlive(120)//wait 2 minute
                .build();
        jobManager = new JobManager(this, configuration);
    }
}


3.创建Job任务

public class MJob extends Job {
    private static final String TAG = "LeeTest------->";
    private String text;

    /**
     * 初始化任务,将text作为优先级,也作为任务号
     * @param text 必须为整形的字符串 需要作为job的优先级使用 比如 1
     *             {@code com.example.li.leeandroidpriorityjobqueue. MJob#onAdded}
     */
    public MJob(String text) {
        super(new Params(Integer.parseInt(text)).persist());
        this.text = text;
        Log.i(TAG, text + "  init");
    }

    @Override
    public void onAdded() {
        Log.i(TAG, text + "  Onadded to task list");
    }

    @Override
    public void onRun() throws Throwable {
        Log.i(TAG, text + "  onRun");
        Thread.sleep(1000);
    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount,int maxRunCount) {
        return RetryConstraint.createExponentialBackoff(runCount, 1000);
    }

    @Override
    protected void onCancel() {
        Log.i(TAG, text + "  onCancel");
    }
}

4.MainActivity

public class MainActivity extends AppCompatActivity {
    private JobManager jobManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        jobManager = AppApplication.getInstance().getJobManager();
        Button btn_start = (Button) findViewById(R.id.btn_start);
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jobManager.addJobInBackground(new MJob("1"));
                jobManager.addJobInBackground(new MJob("2"));
                jobManager.addJobInBackground(new MJob("3"));
                jobManager.addJobInBackground(new MJob("4"));
                jobManager.addJobInBackground(new MJob("5"));
                jobManager.addJobInBackground(new MJob("6"));
                jobManager.addJobInBackground(new MJob("7"));
                jobManager.addJobInBackground(new MJob("8"));
                jobManager.addJobInBackground(new MJob("9"));
                jobManager.addJobInBackground(new MJob("18"));
                jobManager.addJobInBackground(new MJob("28"));
                jobManager.addJobInBackground(new MJob("38"));
                jobManager.addJobInBackground(new MJob("48"));
                jobManager.addJobInBackground(new MJob("58"));
                jobManager.addJobInBackground(new MJob("68"));
                jobManager.addJobInBackground(new MJob("78"));
                jobManager.addJobInBackground(new MJob("88"));
            }
        });
    }
}

添加权限

<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

设置application
<application
    android:name=".AppApplication"

code : https://github.com/HungryGoogle/LeeArchtecture.git

4.执行结果 

09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 1  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 2  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 3  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 4  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 5  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 6  init
09-06 09:20:56.235 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 7  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 8  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 9  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 18  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 28  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 38  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 48  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 58  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 68  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 78  init
09-06 09:20:56.236 15211-15211/com.example.androidpriorityjobqueue I/LeeTest------->: 88  init
09-06 09:20:56.245 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 1  Onadded to task list
09-06 09:20:56.259 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 1  onRun
09-06 09:20:56.265 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 2  Onadded to task list
09-06 09:20:56.269 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 2  onRun
09-06 09:20:56.275 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 3  Onadded to task list
09-06 09:20:56.284 15211-15277/com.example.androidpriorityjobqueue I/LeeTest------->: 3  onRun
09-06 09:20:56.289 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 4  Onadded to task list
09-06 09:20:56.298 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 5  Onadded to task list
09-06 09:20:56.306 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 6  Onadded to task list
09-06 09:20:56.315 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 7  Onadded to task list
09-06 09:20:56.323 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 8  Onadded to task list
09-06 09:20:56.332 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 9  Onadded to task list
09-06 09:20:56.340 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 18  Onadded to task list
09-06 09:20:56.349 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 28  Onadded to task list
09-06 09:20:56.357 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 38  Onadded to task list
09-06 09:20:56.366 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 48  Onadded to task list
09-06 09:20:56.374 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 58  Onadded to task list
09-06 09:20:56.386 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 68  Onadded to task list
09-06 09:20:56.399 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 78  Onadded to task list
09-06 09:20:56.410 15211-15276/com.example.androidpriorityjobqueue I/LeeTest------->: 88  Onadded to task list
09-06 09:20:57.279 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 88  onRun
09-06 09:20:57.286 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 78  onRun
09-06 09:20:57.299 15211-15277/com.example.androidpriorityjobqueue I/LeeTest------->: 68  onRun
09-06 09:20:58.303 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 58  onRun
09-06 09:20:58.312 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 48  onRun
09-06 09:20:58.321 15211-15277/com.example.androidpriorityjobqueue I/LeeTest------->: 38  onRun
09-06 09:20:59.329 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 28  onRun
09-06 09:20:59.337 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 18  onRun
09-06 09:20:59.343 15211-15277/com.example.androidpriorityjobqueue I/LeeTest------->: 9  onRun
09-06 09:21:00.346 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 8  onRun
09-06 09:21:00.354 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 7  onRun
09-06 09:21:00.358 15211-15277/com.example.androidpriorityjobqueue I/LeeTest------->: 6  onRun
09-06 09:21:01.376 15211-15279/com.example.androidpriorityjobqueue I/LeeTest------->: 5  onRun
09-06 09:21:01.386 15211-15278/com.example.androidpriorityjobqueue I/LeeTest------->: 4  onRun


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值