简体   繁体   English

从 AsyncTask 更新 Activity

[英]Updating an Activity from an AsyncTask

So I'm an iOS developer learning android, and I'm having a difficult time understanding some things.所以我是一名学习 android 的 iOS 开发人员,我很难理解一些事情。 Right now I have a datamanager class.现在我有一个数据管理器 class。 In that class it has an AsyncTask to update the data.在那个 class 中,它有一个 AsyncTask 来更新数据。 OnPreExecute I pop an activity to show it is updating. OnPreExecute 我弹出一个活动以显示它正在更新。 I understand I could use extras to pass initial information to the UpdateActivity.我知道我可以使用附加功能将初始信息传递给 UpdateActivity。 My problem is I'm not sure how to send new information in OnProgressUpdate.我的问题是我不确定如何在 OnProgressUpdate 中发送新信息。 Here is my code widdled down:这是我的代码:

    private class myTask extends AsyncTask<Integer,String,Void>{

    @Override
    protected void onCancelled() {
        super.onCancelled();
        isUpdating = false;
    }

    @Override
    protected Void doInBackground(Integer... params) {
        //My BG Code
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Remove updating view
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Intent myIntent = new Intent(mContext,UpdateActivity.class);
        mContext.startActivity(myIntent);
    }
}

AsyncTask is designed to work best when nested in an Activity class. AsyncTask 旨在嵌套在 Activity class 中时工作得最好。

What makes AsyncTask 'special' is that it isn't just a worker thread - instead it combines a worker thread which processes the code in doInBackground(...) with methods which run on the Activity's UI thread - onProgressUpdate(...) and onPostExecute(...) being the most commonly used.使 AsyncTask '特别' 的原因在于它不仅仅是一个工作线程 - 相反,它结合了一个处理doInBackground(...)中的代码的工作线程和在 Activity 的 UI 线程上运行的方法 - onProgressUpdate(...)onPostExecute(...)是最常用的。

By periodically calling updateProgress(...) from doInBackground(...) , the onProgressUpdate(...) method is called allowing it to manipulate the Activity's UI elements (progress bar, text to show name of file being downloaded, etc etc).通过从doInBackground(...)定期调用updateProgress(...) ,调用onProgressUpdate(...)方法允许它操作 Activity 的 UI 元素(进度条、显示正在下载的文件名称的文本等) )。

In short, rather than firing your 'update' Activity from an AsyncTask, your update Activity itself should have a nested AsyncTask which it uses to process the update and publish progress to the UI.简而言之,不是从 AsyncTask 触发“更新”Activity,而是更新 Activity 本身应该有一个嵌套的 AsyncTask,用于处理更新并将进度发布到 UI。

You have two options:你有两个选择:

1) Pass an instance of activity to your AsyncTask constructor in order to invoke some method on it: 1) 将活动实例传递给您的 AsyncTask 构造函数,以便对其调用某些方法:

new MyTask(this).execute();

So, you can do:所以,你可以这样做:

  public MyTask (Activity activity) {
   this.activity = activity;
  }

  public void onPostExecute(...) {
   activity.someMethod();
  }

2) Pass a Handler instance and send message from onPostExecute() to the activity. 2) 传递一个Handler实例并将消息从 onPostExecute() 发送到活动。

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

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