Android:Toast在Service組件中實現其顯示功能

在做Service簡單練習時,在Service中的OnCreate、OnStart、OnDestroy三個方法中都像在Activity中同樣的方法調用了Toast.makeText,並在Acitivy中通過兩個按鈕來調用該服務的onStart和onDestroy方法:

DemoService代碼如下:


[java] view plaincopy
  1. <span style="font-size:16px;">@Override  
  2.     public void onCreate()  
  3.     {  
  4.         super.onCreate();         
  5.         Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();  
  6.     }  
  7.     @Override  
  8.     public void onStart(Intent intent,int startId)  
  9.     {  
  10.         super.onStart(intent, startId);  
  11.         Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();  
  12.     }  
  13.     @Override  
  14.     public void onDestroy(){  
  15.         super.onDestroy();  
  16.         Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();  
  17. }  
  18. </span>  

運行之後,DemoService中的信息都沒有顯示出來。

剛開始以爲所得到的Context不正確,在Service直接調用getApplicationContext()得到的是Service的Context,但是細究來看,Toast應該得到主UI的Context才能顯示,所以找了一下,Google對Toast的說明中,有一句:

A toast can be created and displayed from an Activity or Service. If you create a toast notification from a Service,it appears in front of the Activity currently in focus.

(http://developer.android.com/guide/topics/ui/notifiers/toasts.html)

那麼按照這句來看,service中創建的toast會在Acivity的UI前面聚焦顯示。但爲什麼運行沒有效果呢?再來查看一下makeText方法。




果然還是Context的問題,所以想要toast能夠正常工作,需要在Activity的主線程上運行纔行,那麼如何得到主線程UI的Context呢?可以通過Handler將一個自定義的線程運行於主線程之上。

再來看一下Toast.show方法的src:

[java] view plaincopy
  1. <span style="font-size:16px;">public void show() {  
  2.        ...  
  3.         service.enqueueToast(pkg, tn, mDuration);   //將該toast插入到一個消息隊列中   
  4.         ...  
  5.     }  
  6. </span>  

原理上看,Android中大致上是消息隊列和消息循環,主線程從消息隊列中取得消息並處理。而Handler看作是一個工具類,用來向消息隊列中插入消息。所以我們重構原來的代碼:

[java] view plaincopy
  1. <span style="font-size:16px;">@Override  
  2.     public void onCreate()  
  3.     {  
  4.         super.onCreate();  
  5.         handler=new Handler(Looper.getMainLooper());  
  6.         handler.post(new Runnable(){  
  7.             public void run(){  
  8.                 Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();  
  9.             }  
  10.         });  
  11.     }  
  12.     @Override  
  13.     public void onStart(Intent intent,int startId)  
  14.     {  
  15.         super.onStart(intent, startId);  
  16.         handler=new Handler(Looper.getMainLooper());  
  17.         handler.post(new Runnable(){  
  18.             public void run(){  
  19.                 Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();  
  20.             }  
  21.         });  
  22.     }  
  23.     @Override  
  24.     public void onDestroy(){  
  25.         super.onDestroy();  
  26.         handler=new Handler(Looper.getMainLooper());  
  27.         handler.post(new Runnable(){  
  28.             public void run(){  
  29.                 Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();  
  30.             }  
  31.         });  
  32. </span>  

運行之後的效果如下:




總結:在Android的Framework中使用Toast,要將Toast添加到主線程裏才能正常工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章