国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Service中使用Toast無法正常顯示問題的解決方法

2019-12-12 04:54:33
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Android Service中使用Toast無法正常顯示問題的解決方法。分享給大家供大家參考,具體如下:

在做Service簡(jiǎn)單練習(xí)時(shí),在Service中的OnCreate、OnStart、OnDestroy三個(gè)方法中都像在Activity中同樣的方法調(diào)用了Toast.makeText,并在Acitivy中通過兩個(gè)按鈕來調(diào)用該服務(wù)的onStart和onDestroy方法:

DemoService代碼如下:

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

運(yùn)行之后,DemoService中的信息都沒有顯示出來。

剛開始以為所得到的Context不正確,在Service直接調(diào)用getApplicationContext()得到的是Service的Context,但是細(xì)究來看,Toast應(yīng)該得到主UI的Context才能顯示,所以找了一下,Google對(duì)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中創(chuàng)建的toast會(huì)在Acivity的UI前面聚焦顯示。但為什么運(yùn)行沒有效果呢?再來查看一下makeText方法。

果然還是Context的問題,所以想要toast能夠正常工作,需要在Activity的主線程上運(yùn)行才行,那么如何得到主線程UI的Context呢?可以通過Handler將一個(gè)自定義的線程運(yùn)行于主線程之上。

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

public void show() {    ...    service.enqueueToast(pkg, tn, mDuration);  //將該toast插入到一個(gè)消息隊(duì)列中    ...}

原理上看,Android中大致上是消息隊(duì)列和消息循環(huán),主線程從消息隊(duì)列中取得消息并處理。而Handler看作是一個(gè)工具類,用來向消息隊(duì)列中插入消息。所以我們重構(gòu)原來的代碼:

@Overridepublic 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();      }    });}@Overridepublic 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();      }    });}@Overridepublic 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開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 普兰店市| 彰化市| 文山县| 三明市| 曲阜市| 招远市| 怀化市| 海城市| 鄂托克前旗| 宿松县| 涿州市| 东丰县| 大悟县| 山东| 嘉定区| 喀喇| 和田县| 丰原市| 博罗县| 肇州县| 东光县| 安化县| 新竹县| 叶城县| 齐齐哈尔市| 兴安县| 长泰县| 宁城县| 黔西县| 旌德县| 武定县| 新建县| 明水县| 弋阳县| 邵武市| 庆云县| 陇南市| 革吉县| 克东县| 米易县| 平遥县|