Android 5.0以上Toast不顯示的解決方法
原因分析
用戶使用android 5.0以上的系統(tǒng)在安裝APP時(shí),將消息通知的權(quán)限關(guān)閉掉了。實(shí)際上用戶本意只是想關(guān)閉Notification,但是Toast的show方法中有調(diào)用INotificationManager這個(gè)類,而這個(gè)類在用戶關(guān)閉消息通知權(quán)限的同時(shí)被禁用了,所以我們的吐司無(wú)法顯示。

Toast.show()
效果圖

自定義Toast(上)與Toast(下)比對(duì)
問(wèn)題解決
既然系統(tǒng)不允許我們調(diào)用Toast,那么我們就自立門(mén)戶——自己寫(xiě)一個(gè)Toast出來(lái)。我們總體的思路是:在Activity的布局中添加View實(shí)現(xiàn)Toast的效果。
Toast背景shape定義
我們知道shape的背景是一個(gè)半透明黑色的圓角效果:

Toast
因此我們使用的是shape的corners和solid結(jié)點(diǎn):
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#99000000" /> <corners android:radius="8dp" /> </shape>
定義布局
布局中我們主要使用TextView控件,設(shè)置相應(yīng)的邊距和背景即可,布局代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mbContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="200dp" android:gravity="bottom|center" android:orientation="vertical" android:paddingLeft="50dp" android:paddingRight="50dp"> <LinearLayout android:id="@+id/toast_linear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shape_toastutils_bg" android:gravity="bottom|center" android:orientation="vertical" android:padding="8dp"> <TextView android:id="@+id/mbMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="5dp" android:layout_weight="1" android:gravity="center" android:shadowColor="#BB000000" android:shadowRadius="2.75" android:textSize="12sp" android:textColor="#FFFFFFFF" /> </LinearLayout> </LinearLayout>
java代碼邏輯
自定義Toast的java代碼邏輯主要模仿系統(tǒng)Toast的makeText() 、show()兩個(gè)方法,此外還需要reset()方法,實(shí)現(xiàn)Toast顯示過(guò)程中Activity切換時(shí)context也隨之切換,關(guān)鍵代碼如下:
makeText(Context context, String message, int HIDE_DELAY)方法:
public static ToastUtils makeText(Context context, String message,
int HIDE_DELAY) {
if (mInstance == null) {
mInstance = new ToastUtils(context);
} else {
// 考慮Activity切換時(shí),Toast依然顯示
if (!mContext.getClass().getName().endsWith(context.getClass().getName())) {
mInstance = new ToastUtils(context);
}
}
if (HIDE_DELAY == LENGTH_LONG) {
mInstance.HIDE_DELAY = 2500;
} else {
mInstance.HIDE_DELAY = 1500;
}
mTextView.setText(message);
return mInstance;
}
makeText(Context context, int resId, int HIDE_DELAY)方法
public static ToastUtils makeText(Context context, int resId, int HIDE_DELAY) {
String mes = "";
try {
mes = context.getResources().getString(resId);
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return makeText(context, mes, HIDE_DELAY);
}
show()方法
public void show() {
if (isShow) {
// 若已經(jīng)顯示,則不再次顯示
return;
}
isShow = true;
// 顯示動(dòng)畫(huà)
mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
// 消失動(dòng)畫(huà)
mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mFadeOutAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation
.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// 消失動(dòng)畫(huà)后更改狀態(tài)為 未顯示
isShow = false;
}
@Override
public void onAnimationEnd(Animation animation) {
// 隱藏布局,不使用remove方法為防止多次創(chuàng)建多個(gè)布局
mContainer.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mContainer.setVisibility(View.VISIBLE);
mFadeInAnimation.setDuration(ANIMATION_DURATION);
mContainer.startAnimation(mFadeInAnimation);
mHandler.postDelayed(mHideRunnable, HIDE_DELAY);
}
方法調(diào)用
自定義Toast的使用與系統(tǒng)Toast類似,調(diào)用方法如下:
ToastUtils.makeText(context, "消息內(nèi)容",ToastUtils.LENGTH_SHORT).show();
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能有所幫助,如果有疑問(wèn)大家可以留言交流。
- Android Toast的用法總結(jié)(五種用法)
- Android使用Toast顯示消息提示框
- Android中使用Toast.cancel()方法優(yōu)化toast內(nèi)容顯示的解決方法
- Android控件系列之Toast使用介紹
- android之自定義Toast使用方法
- Android開(kāi)發(fā)技巧之永不關(guān)閉的Toast信息框(長(zhǎng)時(shí)間顯示而非系統(tǒng)關(guān)閉)
- 超簡(jiǎn)單實(shí)現(xiàn)Android自定義Toast示例(附源碼)
- android自定義Toast設(shè)定顯示時(shí)間
- 如何解決android Toast重復(fù)顯示
- Android9.0上針對(duì)Toast的特殊處理圖文詳解
相關(guān)文章
Android控件AppWidgetProvider使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android控件AppWidgetProvider的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)放大鏡效果的方法實(shí)例(附源碼)
這篇文章主要給大家介紹了利用Android實(shí)現(xiàn)放大鏡效果的方法實(shí)例,文中給出了詳細(xì)的介紹和示例代碼,文章的結(jié)尾更是給出了源碼供大家下載學(xué)習(xí),有需要的朋友們下面來(lái)一起看看吧。2017-01-01
android 仿微信demo——微信啟動(dòng)界面實(shí)現(xiàn)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
快速解決Android適配底部返回鍵等虛擬鍵盤(pán)的問(wèn)題
今天小編就為大家分享一篇快速解決Android適配底部返回鍵等虛擬鍵盤(pán)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android持久化技術(shù)之SharedPreferences存儲(chǔ)實(shí)例詳解
這篇文章主要介紹了Android持久化技術(shù)之SharedPreferences存儲(chǔ),結(jié)合實(shí)例形式較為詳細(xì)的分析了SharedPreferences存儲(chǔ)的原理、應(yīng)用及具體實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01
Android Activity的生命周期與啟動(dòng)模式全面解讀
雖然說(shuō)我們天天都在使用Activity,但是你真的對(duì)Activity的生命機(jī)制完全了解了嗎?Activity的生命周期方法只有七個(gè),但是其實(shí)那只是默認(rèn)的情況。也就是說(shuō)在其他情況下,Activity的生命周期可能不會(huì)是按照我們以前所知道的流程,這就要說(shuō)到Activity的啟動(dòng)模式2021-10-10

