从另一个 AsyncTask doInBackground() 启动 AsyncTask [英] Start AsyncTask from another AsyncTask doInBackground()
问题描述
我正在尝试从另一个 AsyncTask doInBackground()
方法开始一个 AsyncTask...
I'm trying to start an AsyncTask from another AsyncTask's doInBackground()
-method...
这甚至可能吗?如果是,我怎样才能做到这一点?
Is this even possible? If yes, how can I achieve this?
后期
这个想法是我开始一个异步任务,它会从 Tweeter 获取我的状态......一切都很好,直到这里......但是当我遇到一些特定的推文时,我需要用我的服务器上的一些信息来修改它们,在这里我会进行另一个网络操作,因为我需要等到从我的服务器获取信息,所以我这样做:
The idea is that I start an asynctask that will get me statuses from Tweeter ... Everything ok until here ... But when I encounter some specific tweets I need to modify them with some info from my server, here I will make another network operation, and since I need to wait until I get the info from my server I do:
GetContent getContentServiceAsyncTask = new GetContent(context);
try {
tweetText = Uri.decode(getContentServiceAsyncTask.execute(
URL_GET_CONTENT, jsonRequest).get());
} catch (InterruptedException e) {
Log.e(TAG_DEBUG, "InterruptedException: ", e);
} catch (ExecutionException e) {
Log.e(TAG_DEBUG, "ExecutionException: ", e);
}
这是从 doInBackground() 方法中已经启动的 AsyncTask 开始的...
This is started from the allready started AsyncTask in doInBackground() method ...
我知道我可以在 AsyncTask 中添加方法并在 doInBackground() 方法中调用它们,但我需要在其他地方使用它们,我从 onPostExecute 开始这些 AsyncTasks ...
I know I can just add the methods in the AsyncTask and just call them in the doInBackground() method, but I need to use them in other places, where I start these AsyncTasks from onPostExecute ...
如果你们认为有一个简单的解决方法,这不会影响我的表现,那就太好了……如果不是,我将制作一些静态方法,我将在我需要的所有 AsyncTasks 中调用这些方法(但这需要我修改很多代码)
If you guys think there is an easy work-around for this, that won't affect my performance, that will be great ... if not I will make some static methods that I will call in all the AsyncTasks I need(but this will require me to modify a lot of my code)
推荐答案
根据下面的帖子,您可以执行 Activity.runOnUiThread() 在主线程上运行 Runnable(从另一个线程).
According to the post below you can do Activity.runOnUiThread() to run a Runnable on the main-Thread (from another thread).
所以理论上你可以这样做:
So theoretically you could do this:
- 运行异步任务
- 在你的 AsyncTask 中执行 Activity.runOnUiThread(Runnable) 并从这个 runnable 内部启动一个新的 AsyncTask
顾名思义Activity.runOnUiThread()在主线程上执行runnable
As the name says Activity.runOnUiThread() executes the runnable on the main-thread
但它有点hacky.
代码应该是这样的:(没有测试)
Code should look something like this: (didnt test)
// first task
(new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
ParentActitity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//second async stared within a asynctask but on the main thread
(new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
}).execute();
}
});
return null;
}
}).execute();
这个嵌套示例不是在生产中使用的好样式,因为(恕我直言)它几乎不可读.
This nested example is not a good style to use in production because (IMHO) its close to unreadable.
附加说明:
Activity.runOnUiThread(Runnable) 不是静态的!这就是为什么我的示例使用 ParentActivity(.this).runOnUiThread(Runnable).
Activity.runOnUiThread(Runnable) is not static! Thats why my example uses ParentActivity(.this).runOnUiThread(Runnable).
这篇关于从另一个 AsyncTask doInBackground() 启动 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!