[英]Is it possible to run multiple AsyncTask in same time?
I've two activities in my application.我的应用程序中有两个活动。 In my Activity A I'm using one AsyncTask and second Activity B also using another one AsyncTask.
在我的活动 A 中,我使用了一个 AsyncTask,第二个活动 B 也使用了另一个 AsyncTask。 In my Activity A I've upload some data to server and in my Activity B I'm trying to download some other data from server.
在我的活动 A 中,我已将一些数据上传到服务器,而在我的活动 B 中,我正在尝试从服务器下载一些其他数据。 Both these are running in AsyncTask.
这两个都在 AsyncTask 中运行。 My problem is when I trying to download data from server in Activity B
onPreExecute()
method was called but doInBackground()
method was not called it is waiting up to the first Activity A's doInBackground()
action finished.我的问题是当我尝试从活动 B 中的服务器下载数据时,
onPreExecute()
方法被调用但doInBackground()
方法未被调用,它正在等待第一个活动 A 的doInBackground()
操作完成。 Why it is happened?为什么会发生? Is it possible to run more than one background actions at a same time..
是否可以同时运行多个后台操作..
In Activity A在活动 A
ImageButton submit_button = (ImageButton) findViewById(R.id.submit_button);
submit_button.setOnClickListener(new OnClickListener()
{
public void onClick(View record_button)
{
new Save_data().execute();
}
});
class Save_data extends AsyncTask<String, Integer, Integer>
{
protected void onPreExecute()
{
}
protected Integer doInBackground(String... arg0)
{
//uploading data here
}
}
In my Activity B在我的活动 B
ImageButton get_button = (ImageButton) findViewById(R.id.get_button);
get_button.setOnClickListener(new OnClickListener()
{
public void onClick(View record_button)
{
new download_process().execute();
}
});
class download_process extends AsyncTask<String, integer, integer>
{
protected void onPreExecute()
{
Log.e("pre-execute","has been called");//This Log works well
}
protected Integer doInBackground(String... arg0)
{
//downloading data here
}
}
Yes, it is, but since Honeycomb, there's a change in the way AsyncTask
is handled on Android.是的,确实如此,但是自从 Honeycomb 以来,
AsyncTask
在 Android 上的处理方式发生了变化。 From HC+ they are executed sequentially, instead of being fired in parallel, as it used to be.从 HC+ 开始,它们按顺序执行,而不是像以前那样并行触发。 Solution for this is to run
AsyncTask
using THREAD_POOL_EXECUTOR
executor explicitely.解决方案是使用
THREAD_POOL_EXECUTOR
执行程序显式运行AsyncTask
。 You can create helper class for that:您可以为此创建助手类:
public class AsyncTaskTools {
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
execute(task, (P[]) null);
}
@SuppressLint("NewApi")
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
}
and then you can simply call:然后你可以简单地调用:
AsyncTaskTools.execute( new MyAsyncTask() );
or with params (however I rather suggest passign params via task constructor):或使用 params (不过我更建议通过任务构造函数 passign params ):
AsyncTaskTools.execute( new MyAsyncTask(), <your params here> );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.