您需要在活动类中创建一个扩展AsyncTask
的类。
UpdateDBTask task = new UpdateDBTask();
task.execute(someString);
在你的异步任务可以定义3个变量 - (所有这些都不可能是原始:意int
例如必须是Integer
)。
首先是您发送给异步任务对象以在doInBackground()
中使用的内容。 其次是你用来更新你的主线程onProgressUpdate()
。 三是doInBackground()
返回,将得到并用于显示结果(再次 - 在主线程中)。你不必使用其中的任何一个(在我给你的代码中使用LIKE),但是在扩展AsyncTask
时必须写入类型。
public class UpdateDBTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
//Everything written here will happen in main thread before doInBackground() starts.
}
@Override
protected String doInBackground(String... params) {
//Do your things in different thread, allowing the main
//thread change things on GUI (Like showing toast...)
return null;
}
@Override
protected void onPostExecute(String result) {
//Everything you do here happens in the main thread AFTER doInBackground() is done.
}
}
您可以执行阻断UI线程操作(例执行netwok操作等),所以面包不会显示出来utill UI线程可以自由 – Pragnani 2013-04-28 15:32:25
尝试使用的AsyncTask为您的网络操作或数据库更新,并在onPreExecute()' – Houcine 2013-04-28 15:45:40
中显示你的Toast会不会有一个while循环有这种效果? – 2013-04-28 15:55:05