美文网首页
一个好用的几乎没有Callback的Android异步库

一个好用的几乎没有Callback的Android异步库

作者: gp_libs | 来源:发表于2017-03-13 20:43 被阅读0次

    android-async-task

    这是一个Android平台处理复杂异步任务的库
    github传送门: https://github.com/gplibs/android-async-task

    1. 安装方法

    gradle:

    dependencies {
        compile 'com.gplibs:task:1.0.0'
    }
    

    Application onCreate:

    public class App extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            
            TaskProxy.init(getApplicationContext());
        }
    }
    

    <br />


    2. 一个简单例子

    imageimage

    如图 Task1, Task2, Task3 为一组,按先后顺序执行, 其中Task2在主线程执行, Task1, Task3在后台线程执行。

    Task4, Task5 为二组,按先后顺序执行,都在后台线程执行。

    一组和二组同时启动,都执行完成后,开始Task6(Task6在主线程执行)。

    <br />

    以下是使用该库实现上面过程的代码:

    TasksSample:

    public class TasksSample extends TaskProxy<TasksSample> {
    
        @TaskGroup(1)
        protected Task<Void> task1() {
            Log.d("TASK", "Task1 begin - isUIThread:" + isUIThread());
            sleep(1000);
            Log.d("TASK", "Task1 end");
            return VoidTask();
        }
    
        @UIThread
        @TaskGroup(1)
        protected Task<Void> task2() {
            Log.d("TASK", "Task2 begin - isUIThread:" + isUIThread());
            sleep(1000);
            Log.d("TASK", "Task2 end");
            return VoidTask();
        }
    
        @TaskGroup(1)
        protected Task<Void> task3() {
            Log.d("TASK", "Task3 begin - isUIThread:" + isUIThread());
            sleep(1000);
            Log.d("TASK", "Task3 end");
            return VoidTask();
        }
    
        @TaskGroup(2)
        protected Task<Void> task4() {
            Log.d("TASK", "Task4 begin - isUIThread:" + isUIThread());
            sleep(500);
            Log.d("TASK", "Task4 end");
            return VoidTask();
        }
    
        @TaskGroup(2)
        protected Task<Void> task5() {
            Log.d("TASK", "Task5 begin - isUIThread:" + isUIThread());
            sleep(2500);
            Log.d("TASK", "Task5 end");
            return VoidTask();
        }
    
        @UIThread
        protected Task<Void> task6() {
            Log.d("TASK", "Task6 begin - isUIThread:" + isUIThread());
            sleep(100);
            Log.d("TASK", "Task6 end");
            return VoidTask();
        }
    
        public Task<Void> doTasks() {
            Log.d("TASK", "TasksSample tasks begin");
            TaskFactory.startSync(
                    getProxy().task1(),
                    getProxy().task2(),
                    getProxy().task3(),
                    getProxy().task4(),
                    getProxy().task5()
            );
            getProxy().task6().startSync();
            Log.d("TASK", "TasksSample tasks end");
            return VoidTask();
        }
    
        private void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        private boolean isUIThread() {
            return Thread.currentThread().getId() == Looper.getMainLooper().getThread().getId();
        }
    }
    

    调用 TasksSample:

    public class TasksActivity extends AppCompatActivity {
    
        TasksSample mSample = new TasksSample();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tasks);
    
            test();
        }
    
        private void test() {
            Log.d("TASK", "Activity test begin");
            mSample.getProxy().doTasks().startAsync(new TaskCallback<Task<Void>>() {
                @Override
                public void run(Task<Void> task) {
                    Log.d("TASK", "doTasks callback");
                }
            });
            Log.d("TASK", "Activity test end");
        }
    }
    

    运行结果:

    imageimage

    如上 书写起来非常简洁方便;

    @TaskGroup 给任务分组, @UIThread 标识在主线程执行;

    多个分组的任务直接丢进 TaskFactory 内部自动根据注解执行(同一组的任务按放进的先后顺序执行)。

    <br />


    3. 有返回值的任务

    ResultTaskSample:

    public class ResultTaskSample extends TaskProxy<ResultTaskSample> {
    
        public Task<String> resultTask() {
            sleep(1000);
            return Task("string result");
        }
    
        private void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    

    调用 ResultTaskSample:

    ResultTaskSample sample = new ResultTaskSample();
    
    ...
    
    // 同步调用(会阻塞调用线程)
    Task<String> task = sample.getProxy().resultTask().startSync();
    String result = task.getResult();
    Log.d("TASK", "result is:" + result);
    
    ...
    
    // 异步调用
    sample.getProxy().resultTask().startAsync(new TaskCallback<Task<String>>() {
        @Override
        public void run(Task<String> task) {
            String result = task.getResult();
            Log.d("TASK", "result is:" + result);
        }
    });
    
    

    <br />


    4. 处理任务中发生的异常

    ResultTaskSample sample = new ResultTaskSample();
    
    ...
    
    // 同步调用(会阻塞调用线程)
    Task<String> task = sample.getProxy().resultTask().startSync();
    if (task.isSuccess()) {
        String result = task.getResult();
        Log.d("TASK", "result is:" + result);
    } else {
        // 打印异常信息
        task.getThrowable().printStackTrace();
    }
    
    ...
    
    // 异步调用
    sample.getProxy().resultTask().startAsync(new TaskCallback<Task<String>>() {
        @Override
        public void run(Task<String> task) {
            if (task.isSuccess()) {
                String result = task.getResult();
                Log.d("TASK", "result is:" + result);
            } else {
                // 打印异常信息
                task.getThrowable().printStackTrace();
            }
        }
    });
    
    

    <br />


    5. 多任务事件

    当用TaskFactory启动多个任务时, 可以通过 TaskEventListener 获知各 Task 的执行情况。

    TaskEventSample:

    public class TaskEventSample extends TaskProxy<TaskEventSample> {
    
        protected Task<Void> task1() {
            Log.d("TASK", "Task1 begin");
            sleep(500);
            Log.d("TASK", "Task1 end");
            return VoidTask();
        }
    
        protected Task<Void> task2() {
            Log.d("TASK", "Task2 begin");
            sleep(1000);
            Log.d("TASK", "Task2 end");
            return VoidTask();
        }
    
        private void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    

    调用 TaskEventSample:

    TaskEventSample sample = new TaskEventSample();
    
    ...
    
    TaskEventListener listener = new TaskEventListener() {
        @Override
        public void onAllTaskCompleted() {
            Log.d("TASK", "all task completed");
        }
        @Override
        public void onTaskCompleted(Task<?> task) {
            Log.d("TASK", "A task completed, tag is:" + task.getTag());
        }
    };
    TaskFactory.startAsync(
            listener,
            sample.getProxy().task1().tag("1"),
            sample.getProxy().task2().tag("2")
    );
    

    相关文章

      网友评论

          本文标题:一个好用的几乎没有Callback的Android异步库

          本文链接:https://www.haomeiwen.com/subject/xmmqnttx.html