Android Service中使用Toast無(wú)法正常顯示問(wèn)題的解決方法
本文實(shí)例講述了Android Service中使用Toast無(wú)法正常顯示問(wèn)題的解決方法。分享給大家供大家參考,具體如下:
在做Service簡(jiǎn)單練習(xí)時(shí),在Service中的OnCreate、OnStart、OnDestroy三個(gè)方法中都像在Activity中同樣的方法調(diào)用了Toast.makeText,并在Acitivy中通過(guò)兩個(gè)按鈕來(lái)調(diào)用該服務(wù)的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();
}
運(yùn)行之后,DemoService中的信息都沒(méi)有顯示出來(lái)。
剛開(kāi)始以為所得到的Context不正確,在Service直接調(diào)用getApplicationContext()得到的是Service的Context,但是細(xì)究來(lái)看,Toast應(yīng)該得到主UI的Context才能顯示,所以找了一下,Google對(duì)Toast的說(shuō)明中,有一句:
“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)
那么按照這句來(lái)看,service中創(chuàng)建的toast會(huì)在Acivity的UI前面聚焦顯示。但為什么運(yùn)行沒(méi)有效果呢?再來(lái)查看一下makeText方法。

果然還是Context的問(wèn)題,所以想要toast能夠正常工作,需要在Activity的主線程上運(yùn)行才行,那么如何得到主線程UI的Context呢?可以通過(guò)Handler將一個(gè)自定義的線程運(yùn)行于主線程之上。
再來(lái)看一下Toast.show方法的src:
public void show() {
...
service.enqueueToast(pkg, tn, mDuration); //將該toast插入到一個(gè)消息隊(duì)列中
...
}
原理上看,Android中大致上是消息隊(duì)列和消息循環(huán),主線程從消息隊(duì)列中取得消息并處理。而Handler看作是一個(gè)工具類,用來(lái)向消息隊(duì)列中插入消息。所以我們重構(gòu)原來(lái)的代碼:
@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();
}
});
}
運(yùn)行之后的效果如下:

總結(jié):在Android的Framework中使用Toast,要將Toast添加到主線程里才能正常工作。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
詳解關(guān)于Android Studio中安裝和gradle的一些坑
本篇文章主要介紹了關(guān)于Android Studio中安裝和gradle的一些坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接
本文主要介紹Android IPC通信Binder中的Server和Client獲得Service Manager接口,這里詳細(xì)的說(shuō)明了如何實(shí)現(xiàn)Service Manager接口,對(duì)研究Android源碼的朋友提供幫助,有需要的小伙伴可以參考下2016-08-08
簡(jiǎn)述Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法
本篇文章主要對(duì)Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法進(jìn)行介紹,相信對(duì)大家學(xué)習(xí)會(huì)有很好的幫助,需要的朋友一起來(lái)看下吧2016-12-12
Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡(jiǎn)單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09
Android RecyclerView的焦點(diǎn)記憶封裝
這篇文章主要介紹了Android RecyclerView的焦點(diǎn)記憶封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
android如何設(shè)置小區(qū)廣播默認(rèn)信道(50與60并支持雙卡)
置小區(qū)廣播默認(rèn)信道50與60,并支持雙卡主要是印度市場(chǎng),具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android kotlin語(yǔ)言實(shí)現(xiàn)刪除文件的解決方案
這篇文章主要介紹了Android kotlin語(yǔ)言實(shí)現(xiàn)刪除文件的解決方案,在項(xiàng)目開(kāi)發(fā)過(guò)程中,尤其是需要跨平臺(tái)協(xié)作的項(xiàng)目,那么刪除用戶指定的文件的這種操作就顯得尤為重要了,需要的朋友可以參考下2025-02-02
Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
這篇文章主要介紹了Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

