Android:在Service中使用Toast

来源:互联网 发布:淘宝刷收藏 编辑:程序博客网 时间:2024/05/21 09:06

   这个再次说明,所有涉及UI的更新的操作,必须放在UI线程中。

  转自:http://bbs.chinaunix.net/thread-3653294-1-1.html

在Service中Toast,本来以为是和在Activity中一样直接用,结果发现没有反应,百度到的解决办法,原理暂时不清楚。 

  1. 1.public class TestService extends  Service {  
  2. 2.    private Handler handler;  
  3. 3.    @Override  
  4. 4.    public IBinder onBind(Intent intent){  
  5. 5.        return null;  
  6. 6.    }  
  7. 7.      
  8. 8.    @Override  
  9. 9.    public void onCreate(){  
  10. 10.        handler = new Handler(Looper.getMainLooper());                          
  11. 11.        System.out.println("service started");  
  12. 12.        handler.post(new Runnable() {    
  13. 13.             @Override    
  14. 14.             public void run() {    
  15. 15.                Toast.makeText(getApplicationContext(), "Test",Toast.LENGTH_SHORT).show();    
  16. 16.             }    
  17. 17.        });  
  18. 18.    }  
  19. 19.}   
Show toast at current Activity from service

Try using a Handler. Thing about Toasts is, you have to run makeText on the UI thread, which the Service doesn't run on. A Handler allows you to post a runnable to be run on the UI thread. In this case you would initialize a Handler in your onStartCommand method.

private Handler mHandler;@OverrideonStartCommand(...) {  mHandler = new Handler();}private class ToastRunnable implements Runnable {    String mText;    public ToastRunnable(String text) {        mText = text;    }    @Override    public void run(){         Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show();    }}private void someMethod() {    mHandler.post(new ToastRunnable(<putTextHere>);}

  另一个方法:http://www.androidsnippets.com/use-toast-wherever-you-want

Often times, you need to pop up a Toast message to describe something - whether you're in the context of the main GUI thread or not. However, you can only use Toast in the main GUI thread, or else you run into problems where the Toast message doesn't disappear after a period (because the main GUI context doesn't know anything about Toast messages used in a separate thread context).

So, to be able to use Toast wherever you want, you have to be able to post the message in the main GUI thread. To do this, you can use AsyncTask.

raw ·
copy
· download
   // A class that will run Toast messages in the main GUI context   private class ToastMessageTask extends AsyncTask<String, String, String> {    String toastMessage;    @Override    protected String doInBackground(String... params) {        toastMessage = params[0];        return toastMessage;    }    protected void OnProgressUpdate(String... values) {         super.onProgressUpdate(values);    }   // This is executed in the context of the main GUI thread    protected void onPostExecute(String result){           Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);           toast.show();    }}// to use:    new ToastMessageTask().execute("This is a TOAST message!");

0 0