简体   繁体   English

如何从ASyncTask调用父活动功能?

[英]How to call parent activity function from ASyncTask?

setAccountAuthenticatorResult can be called from the Activity, which extends AccountAuthenticatorActivity . 可以从Activity调用setAccountAuthenticatorResult ,它扩展了AccountAuthenticatorActivity My activity extends that, but launches ASyncTask and hence this setAccountAuthenticatorResult should be called from ASyncTask (or, the result of ASyncTask should be passed back to the main thread). 我的活动扩展了,但是启动了ASyncTask,因此应该从ASyncTask调用此setAccountAuthenticatorResult (或者,ASyncTask的结果应该传递回主线程)。

How to do it? 怎么做?

What is wrong in the code below? 下面的代码有什么问题?

AsyncTask<Uri, Void, Bundle> task = new RetrieveAccessTokenTask(this, consumer, provider, prefs).execute(uri);

public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Bundle> {
    private Context context;

    public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,
            OAuthProvider provider, SharedPreferences prefs) {
        this.context = context;
    }

    @Override
    protected void onPostExecute(Bundle result) {
        context.setAccountAuthenticatorResult(); // doesn't work

    }

When you create the AsyncTask, you can add a new constructor to it, and pass in a reference to the Activity: 创建AsyncTask时,可以向其添加新的构造函数,并传入对Activity的引用:

AsyncTask myTask = new MyTask(this);

And then from the onPostExecute() method in the AsyncTask you can call the method on the Activity. 然后从AsyncTask中的onPostExecute()方法中可以调用Activity上的方法。

public class MyTask extends AsyncTask<String, String, String>
{
    public MyActivity activity;

    public MyTask(MyActivity a)
    {
        this.activity = a;
    }

    //  ......

    protected void onPostExecute(String result)
    {
        activity.myMethod();
    }
}

Use Interface 使用界面

Follow these steps: 跟着这些步骤:

1) Create an interface 1)创建一个界面

public interface AsyncTaskListener{

   public void updateResult(String result);

}

2) Use the listener in your AsyncTask 2)在AsyncTask中使用监听器

DownloadSongTask extends AsyncTask<String,Integer,String>{

   private AsyncTaskListener listener;

   public DownloadSongTask(Context context)
   {
       listener= (AsyncTaskListener)context;    // Typecast 
   }

   @Override
   public void doInbackGround(String... params)
   {
       // Download code
       int downloadPerc = // calculate that
       publish(downloadPerc);   
   }

   @Override
   public void onPostExecute(String result)
   {
       listener.updateResult(String result);  // Use it 
   }

}

3) Implement the interface in your Activity and Override the interface method 3)在Activity中实现接口并覆盖接口方法

  public class YourActivity extends AppcompatActivity implements AsyncTaskListener{

  // Activity code //
  new  DownloadSongTask(this).execute("Paradise.mp3");  // this is how you start Task

  public void yourMethod(String arg)
  {
    // Your method related Stuff
  }

  @Override 
  public void updateResult(String result){
        yourMethod(result);
  }

}

Advantege of using interface? Advantege使用界面?

This seems a lengthy approach at first but if you use this approach 这似乎是一个漫长的方法,但如果你使用这种方法

You can make a loosely coupled AsyncTask. 你可以做一个松散耦合的AsyncTask。 Which means you can use same AsyncTask with any Activity in Future without even changing code in your AsyncTask. 这意味着你可以在任何Activity in Future中使用相同的AsyncTask,甚至不用更改AsyncTask中的代码。

Relevant Links: 相关链接:

For better understanding you can read this ANSWER 为了更好地理解,您可以阅读此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM