在我的一个活动中,我使用了AsyncTask。在doInBackground()中,我调用了各种方法。我知道我可以使用Log,但我还是更喜欢Toast。那么,如何在doInBackground()中使用AsyncTask中的Toast呢?
发布于 2012-12-10 02:33:11
你可以用runOnUIThread()包装吐司,但这不是最好的解决方案。
当发生错误时,应在catch块中设置一个布尔标志,然后只要该标志为true,就在onProgressUpdate()、onPostExecute()或具有UI访问的任何其它方法中显示适当的Toast。
发布于 2012-12-10 02:41:46
从doInBackground返回为
protected String doInBackground(String... params){
//some code
try{
//some code
}catch(Exception e){
return "Exception Caught";
}
return someValidResult;
}
protected void onPostExecute(String result){
if(result.equalsIgnoreCase("Exception Caught")){
//Display Toast
}else{
// // whatever you wana do with valid result
}
}发布于 2013-11-20 21:37:40
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();
}
});Fragments,则需要通过您的活动调用runOnUiThread(...):getActivity().runOnUiThread(...)
https://stackoverflow.com/questions/13790351
复制相似问题