有没有办法在AsyncTask的doinbackground()中执行UI任务?我很清楚在onPostExecute方法中执行它更好。但在我的情况下,因为我需要使用可重复使用的警报,能够访问我的doinbackground中的用户界面将为我节省很多时间。也就是说,我需要调整46个地方的代码,但能够在doinbackground中做到这一点将需要在一个地方进行更改。在Android中的doinbackground()中执行UI任务
在此先感谢
有没有办法在AsyncTask的doinbackground()中执行UI任务?我很清楚在onPostExecute方法中执行它更好。但在我的情况下,因为我需要使用可重复使用的警报,能够访问我的doinbackground中的用户界面将为我节省很多时间。也就是说,我需要调整46个地方的代码,但能够在doinbackground中做到这一点将需要在一个地方进行更改。在Android中的doinbackground()中执行UI任务
在此先感谢
希望这将解决您的问题
onPreExecute() {
// some code #1
}
doInBackground() {
runOnUiThread(new Runnable() {
public void run() {
// some code #3 (Write your code here to run in UI thread)
}
});
}
onPostExecute() {
// some code #3
}
非常感谢这个例子:) – Lincy
任何时候,如果这有助于upvote ....开心编码:) –
比更新从onPostExecute()
UI其他,有2种方式来更新UI:通过实施runOnUiThread()
onProgressUpdate()
FYI
doInBackground()
, onProgressUpdate()
- 只要您想更新doInBackground()
的任何内容,您只需使用publishProgress(Progress...)
发布一个或多个进度单元,它将ping onProgressUpdate()
以在UI线程上更新。
你可以利用onProgressUpdate
而
http://developer.android.com/reference/android/os/AsyncTask.html。 – Raghunandan
当你创建这个问题并在这里发布时,你可以很容易地编写一些代码并检查发生了什么。 – stinepike
你有什么尝试? –