1. 程式人生 > >Service中使用Toast顯示問題

Service中使用Toast顯示問題

作者:陳旭

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

DemoService程式碼如下:

@Override
	public void onCreate()
	{
		super.onCreate();		
		Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
	}
	@Override
	public void onStart(Intent intent,int startId)
	{
		super.onStart(intent, startId);
		Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}

執行之後,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:

public void show() {
       ...
        service.enqueueToast(pkg, tn, mDuration);   //將該toast插入到一個訊息佇列中
        ...
    }

原理上看,Android中大致上是訊息佇列和訊息迴圈,主執行緒從訊息佇列中取得訊息並處理。而Handler看作是一個工具類,用來向訊息佇列中插入訊息。所以我們重構原來的程式碼:

@Override
	public void onCreate()
	{
		super.onCreate();
		handler=new Handler(Looper.getMainLooper());
		handler.post(new Runnable(){
			public void run(){
				Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
			}
		});
	}
	@Override
	public void onStart(Intent intent,int startId)
	{
		super.onStart(intent, startId);
		handler=new Handler(Looper.getMainLooper());
		handler.post(new Runnable(){
			public void run(){
				Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
			}
		});
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		handler=new Handler(Looper.getMainLooper());
		handler.post(new Runnable(){
			public void run(){
				Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
			}
		});

執行之後的效果如下:


總結:在Android的Framework中使用Toast,要將Toast新增到主執行緒裡才能正常工作。