2013-04-28 38 views
0

点击一个按钮我想显示一个吐司消息,虽然有一些工作正在进行,但即使通过我有它在开始时吐司没有被显示,直到结束敬酒没有在正确的时间显示

if (id == R.id.edit_score_button_update) { 

Toast.makeText(this, "Updating please wait", Toast.LENGTH_LONG).show(); 

//Some code that update database 

finish(); 

什么是强制烤面包首先被显示的最佳方式。感谢您的时间

+4

您可以执行阻断UI线程操作(例执行netwok操作等),所以面包不会显示出来utill UI线程可以自由 – Pragnani 2013-04-28 15:32:25

+0

尝试使用的AsyncTask为您的网络操作或数据库更新,并在onPreExecute()' – Houcine 2013-04-28 15:45:40

+0

中显示你的Toast会不会有一个while循环有这种效果? – 2013-04-28 15:55:05

回答

1

您需要在活动类中创建一个扩展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. 
    } 
} 
+0

谢谢。它做到了。现在更好的工作并不需要敬酒,而是在后台完成 – 2013-05-08 13:47:38