我正在开发一个 android 应用程序,我有一个扩展Thread
类的单独类,在这里我调用一个服务并获取数据,现在我需要知道这个线程何时完成,并在完成时向我显示一个 Toast。比如“成功”
有没有类似ThreadonPostExecute()
的方法?AsyncTask
谢谢
我正在开发一个 android 应用程序,我有一个扩展Thread
类的单独类,在这里我调用一个服务并获取数据,现在我需要知道这个线程何时完成,并在完成时向我显示一个 Toast。比如“成功”
有没有类似ThreadonPostExecute()
的方法?AsyncTask
谢谢
我希望你正在使用这样的线程..
.....
new Thread(new Runnable() {
public void run() {
YourMetod(); //you want to execute first
finishedHandler.sendEmptyMessage(0);//when first method is executed completly you need to call this
}
}).start();
....
像这样在你的类中创建一个处理程序
private Handler finishedHandler = new Handler() {
@Override public void handleMessage(Message msg) {
//create your toast here
}
};
试试这个希望有帮助
Display a toast is different that modify Views component because toast can be displayed from every thread while views need to be accessed only from the main thread.
So, if you need just to display a Thread just call Toast.makeToast(...).show()
wherever you are.
Anyway, you can send messages from a backgrund thread to the main thread using the Handler
class:
http://developer.android.com/reference/android/os/Handler.html
http://developer.android.com/guide/faq/commontasks.html#threading