我尝试在AsyncTask
MainActivity
函数中启动两个onCreate
我的任务是同时启动它们,但我认为在目录扫描过程完成后启动授权过程。为什么呢?
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
task1 = new MyTask1();
task1.execute(); // Asynch ???
task2 = new MyTask2();
task2.execute(); // Asynch ???
class MyTask1 extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
// Disk scan process with using some functions from MainActivity
DiskScanProcess();
} catch (InterruptedException e) {
}
return null;
}
}
class MyTask2 extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
// Authorizatin process with using some functions from MainActivity
AuthorizationProcess();
} catch (InterruptedException e) {
}
return null;
}
}
}
答案 0 :(得分:0)
The documentation for AsyncTask在标题为“执行顺序”的部分中解释说,“从HONEYCOMB开始,任务在单个线程上执行,以避免由并行执行引起的常见应用程序错误”。如果您希望并行执行多个任务,请使用executeOnExecutor()调用它们。