Android的调用AsyncTask的右后一个又一个成品 [英] Android calling AsyncTask right after an another finished

查看:133
本文介绍了Android的调用AsyncTask的右后一个又一个成品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题与Android AsyncTask的。有其中包含一些TextView的一个按钮和一张图片的活动。当用户输入这个活动我启动的AsyncTask来检查用户是否可以对从活动(直到任务完不成按钮无效)。然后,我要开始另asyntask得到的图片。 所以我做了一个内部​​类:

I have some problem with Android AsyncTask. There is an Activity which contains some TextView a button and a picture. When an user entered this activity I start an asynctask to check whether the user can go toward from the activity (until the task not finish the button not active). Then I want to start another asyntask to get the picture. So I made an inner class:

AsyncTask<String, Void, JSONObject>() authTask = new AsyncTask<String, Void, JSONObject>() {
     @Override
     protected JSONObject doInBackground(String... params) {
         //call the rest api
     }
     @Override
     protected void onPostExecute(JSONObject result) {
         // check the result
         // and make another asynctask
         AsyncTask<String, Void, Bitmap> imageTask = new Async.... {
             // get image
         }
         imageTask.execute();
     }
}

和我打电话 authTask.execute(); 从UI线程

and I call authTask.execute(); from the UI thread.

我有关于这一个不好的感觉,尤其​​是它似乎不工作(这是确定几次,但它突然冻结:未见异常只是挂和进度条是纺纱什么也没有发生,按钮也不会。活性。) 还有另一种方式来获得信息,当它结束马上开始另一项任务?

I have a bad feeling about this, especially it seems doesn't work (it's ok few times but suddenly it "freeze": no exception just hanging and the progress bar is spinning. Nothing happens and the button won't be active.) There is another way to get an information and when it's finished immediately start another task?

UDPATE: 我用API级别10工作在authTask我得到它需要开始imageTask(有的ID)的一些信息,所以我必须在连续调用这些任务。在API级别10这是可能的吗?

UDPATE: I working with api level 10. In authTask I get some information which is needed to start imageTask (some id) so I have to call these tasks in a row. In api level 10 it's is possible?

在此先感谢!

BR,彼得·

推荐答案

您可以使用的getStatus()检查是否在的AsyncTask 挂起,运行,或finished.and当末梢开始新的task.like:

you can use getStatus() checks whether the the AsyncTask is pending, running, or finished.and when finsh start your new task.like:

if(authTask .getStatus() == AsyncTask.Status.PENDING){
    // My AsyncTask has not started yet
}

if(authTask .getStatus() == AsyncTask.Status.RUNNING){
    // My AsyncTask is currently doing work in doInBackground()
}

if(authTask .getStatus() == AsyncTask.Status.FINISHED){
    // START NEW TASK HERE
}

例如你的应用程序:

example for your app:

btn.setOnClickListener(new View.OnClickListener()
  {
    public void onClick(View v)
      {
        if (authTask != null && authTask.getStatus() == AsyncTask.Status.FINISHED) {
           //START YOUR NEW TASK HERE
        }
        else
        {
          //IGNORE BUTTON CLICK
        }
      }
   }); 

这篇关于Android的调用AsyncTask的右后一个又一个成品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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