在Android 5.0 +中,禁用通知权限,然后系统的Toast不显示。任何人都知道它为什么会这样做吗?
以下是代码:
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
答案 0 :(得分:1)
我找到了创建新Toast
类的解决方案。
<强>吐司强>
public class Toast {
private Context mContext;
private WindowManager wm;
private int mDuration;
private View mNextView;
public static final int LENGTH_SHORT = 1500;
public static final int LENGTH_LONG = 3000;
public Toast(Context context) {
mContext = context.getApplicationContext();
wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
public static Toast makeText(Context context, CharSequence text,
int duration) {
Toast result = new Toast(context);
View view = android.widget.Toast.makeText(context, text, android.widget.Toast.LENGTH_SHORT).getView();
if (view != null){
TextView tv = (TextView) view.findViewById(android.R.id.message);
tv.setText(text);
}
result.mNextView = view;
result.mDuration = duration;
return result;
}
public static Toast makeText(Context context, int resId, int duration)
throws Resources.NotFoundException {
return makeText(context, context.getResources().getText(resId),duration);
}
public void show() {
if (mNextView != null) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.CENTER | Gravity.CENTER_HORIZONTAL;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Toast;
params.y = dip2px(mContext, 64);
params.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(mNextView, params);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mNextView != null) {
wm.removeView(mNextView);
mNextView = null;
wm = null;
}
}
}, mDuration);
}
}
/**
* dip2px
*
* @param context
* @param dipValue
* @return int
*
*/
private int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}
<强> XML 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/waiting_bg"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/mColor_white"
android:textSize="15sp"/>
答案 1 :(得分:0)
如果这不是您问题的解决方案,但我不小心取消选中应用程序的“显示通知”设置一次。您可以在设备设置/应用程序/管理器中查看此设置。