后ASyncTask.execute()做自Activity X() [英] After ASyncTask.execute() do x() from Activity

查看:119
本文介绍了后ASyncTask.execute()做自Activity X()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我不知道如何去。 基本上,我有一个AsyncTask的类多数民众赞成在做它的业务像往常一样在后台。我想这样做之后,其完成的东西。现在,在你跳跃前进,并说只使用onPostExecute(),那里有一个陷阱。我需要运行的方法是在活动,而不是Task类。

我看到它的方式,我有两个选择。

答:

  CustomTask任务=新CustomTask();
    task.execute(passedParams);
            //when(task.execute完成)
            {
                DOX();
            }
 

我希望我可以这样做是它如此简单,让我查一下当任务,而无需不断查询它的活动的getStatus和()上的活动完成。 我不认为我会得到这个幸运的,但如果任何人有这样做的一种方式,那简直太好了。

B:

传递活动作为paramater的AsyncTask的。这是混乱的,我不乐意使用它,但是从旁白和对象引用,我不知道这是否会

  CustomTask任务=新CustomTask();
    task.execute(passedParams,MyActivity);
 

然后在任务onPostExecute,我可以把它称之为MyActivity.doX();


C:

第三种方式是让AsyncTask的私有类的活动本身,但我真的想保持独立。 Resusability并没有什么 -

在这个有什么想法?

总之,需要DOX()task.execute完成后。任何想法AP preciated。


D:

好吧,我知道我一滚就在这里。我一直在想新的解决方案。类方法或静态方法,它可以从任何地方调用。

 公共类ProfileSettings扩展活动
{
      公共静态无效DOX()
      {
          //逻辑...
      }
}
 

这是AsyncTask的

  MyActivity.doX();
 

解决方案

选项B应工作,有时是一个不错的选择,但有时我用匿名类此。当您从您的活动称之为:

  CustomTask任务=新CustomTask(){
    @覆盖
    保护无效onPostExecute(长效){
        super.onPostExecute(结果);
        MyActivity.this.doX();
    }
}。执行();
 

This is one I'm not sure how to go about. Basically I have an ASyncTask class thats doing its business as usual in the background. I'd like to do something after its finished. Now before you jump ahead and say "just use onPostExecute()", theres a catch. The method I need run is in the activity and not the Task class.

The way I see it, I have 2 options.

A:

    CustomTask task = new CustomTask();
    task.execute(passedParams);
            //when(task.execute is finished)
            {
                doX();
            }

I hope I can do it this way as Its so simple and lets me check when the task is completed without having to constantly poll it for activity and getStatus() on the activity. I don't think I'll get this lucky but If anyone has a way of doing it, that'd be great

B:

Pass the activity as a paramater to the ASyncTask. This is messy and I'm not happy about using it but asides from that and the object reference, I don't know if it will work

    CustomTask task = new CustomTask();
    task.execute(passedParams,MyActivity);

Then in the Tasks onPostExecute, I can just have it call the MyActivity.doX();


C:

A third way would be to make the asynctask a private class in the activity itself but i really would like to keep it separate. Resusability and what not –

Any thoughts on this?

To summarize, Need to doX() after task.execute is finished. Any ideas appreciated.


D:

Ok I know I'm on a roll here. I keep thinking up new solutions. A class method or static method that can be called from any where.

public class ProfileSettings extends Activity
{
      public static void doX()
      {
          //Logic...
      }
}

From AsyncTask

MyActivity.doX();

解决方案

Option B should work and is sometimes a good option, but sometimes I use anonymous classes for this. When you call it from your activity:

CustomTask task = new CustomTask() {
    @Override
    protected void onPostExecute(Long result) {
        super.onPostExecute(result);
        MyActivity.this.doX();
    }
}.execute();

这篇关于后ASyncTask.execute()做自Activity X()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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