简体   繁体   English

Android:如何处理多项任务

[英]Android: How to handle multiple tasks

I want to perform multiple tasks in Android. 我想在Android中执行多个任务。 I know I can use AsyncTask for this. 我知道我可以为此使用AsyncTask。 But my requirement is I want to execute second task only completion of first task. 但我的要求是我只想完成第二项任务就执行第二项任务。 Then will execute the third task only after completion of second task. 然后,仅在第二个任务完成后才执行第三个任务。 How it could be possible. 怎么可能。 For this I am thinking to use multiple AsyncTasks i mean inside doInBackground of first AsynTask I want to start second AsyncTask. 为此,我正在考虑使用多个AsyncTask,我的意思是在第一个AsynTask的doInBackground内部,我想启动第二个AsyncTask。 Similarly inside doInBackground of secondAsynTask I want to start third AsyncTask and so on.... Is it ok if I implement like this with out any performance issue or is there any other way of doing this. 同样,在secondAsynTask的doInBackground内部,我想启动第三个AsyncTask,以此类推。...如果我以这种方式实现而没有任何性能问题,或者是否有任何其他方式可以这样做,可以吗? Please share your thoughts on this. 请分享您对此的想法。

Create multiple AsyncTasks and call them in a chain. 创建多个AsyncTasks并在链中调用它们。

Eg: 例如:

Call the first task: 调用第一个任务:

new FirstTask().execute();

Then FirstTask class overrides onPostExecute where it calls the second class: 然后,FirstTask类将覆盖onPostExecute并在其中调用第二个类:

new SecondTask().execute()

and so on. 等等。

If you don't want to reinvent the wheel you could use concat () operator of rxjava. 如果您不想重新发明轮子,可以使用rxjava的concat()运算符。

https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#concat https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#concat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM