从 AsyncTask 更新 Activity
所以我是一名学习 Android 的 iOS 开发人员,我很难理解一些事情。现在我有一个数据管理器课程。在该类中,它有一个 AsyncTask 来更新数据。 OnPreExecute 我弹出一个活动以显示它正在更新。我知道我可以使用额外功能将初始信息传递给 UpdateActivity。我的问题是我不确定如何在 OnProgressUpdate 中发送新信息。这是我的代码:
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);
}
}
So I'm an iOS developer learning android, and I'm having a difficult time understanding some things. Right now I have a datamanager class. In that class it has an AsyncTask to update the data. OnPreExecute I pop an activity to show it is updating. I understand I could use extras to pass initial information to the UpdateActivity. My problem is I'm not sure how to send new information in 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AsyncTask 被设计为嵌套在 Activity 类中时效果最佳。
AsyncTask 的“特别”之处在于,它不仅仅是一个工作线程,而是将处理
doInBackground(...)
中的代码的工作线程与在 Activity 的 UI 线程上运行的方法结合起来。 -onProgressUpdate(...)
和onPostExecute(...)
是最常用的。通过定期从
doInBackground(...)
调用updateProgress(...)
,会调用onProgressUpdate(...)
方法,从而允许它操作 Activity 的 UI 元素(进度条、显示正在下载的文件名称的文本等)。简而言之,您的更新活动本身应该有一个嵌套的 AsyncTask,它用来处理更新并将进度发布到 UI,而不是从 AsyncTask 触发您的“更新”活动。
AsyncTask is designed to work best when nested in an 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(...)
andonPostExecute(...)
being the most commonly used.By periodically calling
updateProgress(...)
fromdoInBackground(...)
, theonProgressUpdate(...)
method is called allowing it to manipulate the Activity's UI elements (progress bar, text to show name of file being downloaded, etc etc).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.
您有两个选择:
1)将 Activity 实例传递给 AsyncTask 构造函数,以便调用其上的某些方法:
因此,您可以执行
以下操作:2)传递
Handler
实例并将消息从 onPostExecute() 发送到活动。You have two options:
1) Pass an instance of activity to your AsyncTask constructor in order to invoke some method on it:
So, you can do:
2) Pass a
Handler
instance and send message from onPostExecute() to the activity.