欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年10月28日 11:34:11   作者:pku_android  
這篇文章主要介紹了Android Service中使用Toast無法正常顯示問題的解決方法,分析了Service中Toast無法正常顯示的原因與相關(guān)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下

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

在做Service簡單練習(xí)時,在Service中的OnCreate、OnStart、OnDestroy三個方法中都像在Activity中同樣的方法調(diào)用了Toast.makeText,并在Acitivy中通過兩個按鈕來調(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中的信息都沒有顯示出來。

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

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

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

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

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

@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開發(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é)

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

相關(guān)文章

最新評論