开始的AsyncTask从另一个AsyncTask的doInBackground() [英] Start AsyncTask from another AsyncTask doInBackground()

查看:147
本文介绍了开始的AsyncTask从另一个AsyncTask的doInBackground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动的的AsyncTask 的从其他的的AsyncTask的 doInBackground() -method ...

I'm trying to start an AsyncTask from another AsyncTask's doInBackground()-method...

这甚至可能吗?如果是的话,我怎么能做到这一点?

Is this even possible? If yes, how can I achieve this?

在以后编辑:

我们的想法是,我开始一个AsyncTask的,这将让我从高音单元的状态......一切就OK了,直到这里......但是当我遇到一些具体的鸣叫,我需要从我的服务器修改他们提供一些信息,在这里我将再拍网络运行,因为我需要等待,直到我从我的服务器我做的信息:

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);
}

这是从媒体链接开始AsyncTask的开始doInBackground()方法...

This is started from the allready started AsyncTask in doInBackground() method ...

我知道我可以再补充的方法,在AsyncTask的,只是叫他们在doInBackground()方法,但我需要使用它们在其他地方,在那里我开始这些AsyncTasks从onPostExecute ...

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调用(但是这将需要我修改了很多我的code)

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).

  • <一个href="http://stackoverflow.com/questions/11123621/running-$c$c-in-main-thread-from-another-thread">Running从另一个线程在主线程code

所以,理论上你可以这样做:

So theoretically you could do this:

  • 运行异步任务
  • Activity.runOnUiThread(的Runnable 的),您的AsyncTask里面并从这个可运行
  • 里面开始新的AsyncTask
  • Run the Async Task
  • do Activity.runOnUiThread(Runnable) inside your AsyncTask and start a new AsyncTask from inside of this runnable

正如名字所示,Activity.runOnUiThread()执行的可运行的主线程

As the name says Activity.runOnUiThread() executes the runnable on the main-thread

但它是一种哈克。

code应该是这个样子:(没有测试)

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(可运行)是的不可以静!这就是为什么我的示例使用ParentActivity(。这).runOnUiThread(Runnable接口)。

Activity.runOnUiThread(Runnable) is not static! Thats why my example uses ParentActivity(.this).runOnUiThread(Runnable).

这篇关于开始的AsyncTask从另一个AsyncTask的doInBackground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆