【问题标题】:In Android 5.0+,System's Toast do not show在 Android 5.0+ 中,系统的 Toast 不显示
【发布时间】:2017-11-10 05:50:49
【问题描述】:

在Android 5.0+中,禁用通知权限然后系统的Toast不显示。有人知道这是为什么吗?

代码如下:

Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

【问题讨论】:

标签: android android-toast


【解决方案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"/>

【讨论】:

  • 如果您要粘贴某人的答案,则必须添加学分。或者最后添加链接
  • 谢谢你的帮助,这个方法解决了我的问题。@KeLiuyue
【解决方案2】:

如果这不能解决您的问题,但我不小心取消了一次应用程序的“显示通知”设置。进入您的设备设置/应用程序/管理器并检查此设置可能是一个想法。

【讨论】:

  • 感谢您的帮助,但我是 Android 开发者,您的解决方案可能无法解决我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
相关资源
最近更新 更多