禁用通知还会禁用应用的Toast消息

时间:2016-09-27 10:26:11

标签: android android-notifications android-toast

在我的Android应用程序中,我已经实现了推送通知。我的问题来自我的设备,如果我禁用应用程序的通知消息,甚至吐司消息被禁用。这是通常的现象还是我需要改变什么?请让我知道如何克服这一点。

2 个答案:

答案 0 :(得分:1)

是的,这是一种常见的现象。点击here但它是中文的。希望这可以帮到你。

在这种情况下,需要自己写一个Toast。我可以提供一个演示,但它不完整。你可以做一些修改,使它更好。

public class MToast {
    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 MToast(Context context) {
    mContext = context.getApplicationContext();
    wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}

public static MToast makeText(Context context, CharSequence text,
                             int duration) {
    MToast result = new MToast(context);
    LayoutInflater inflate = (LayoutInflater) context
            .getApplicationContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(R.layout.dialog_toast,null);
    TextView tv = (TextView) v.findViewById(R.id.tvMsg);
    tv.setText(text);
    result.mNextView = v;
    result.mDuration = duration;
    return result;
}

public static MToast 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.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);
    }
}

/**
 * transfer dp into px
 *
 * @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 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:background="@drawable/waiting_bg"
    android:layout_gravity="center"
    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:textSize="15sp"
    android:textColor="@color/mColor_white"
    />

我是markdown.Sorry的新人。

答案 1 :(得分:1)

在2018年写一些额外信息;可能会帮助某人。

因此,每当您通过系统设置禁用应用通知时,Toast消息都会停止为您的应用显示。 但是这种行为似乎是制造商最终的选择。

我在小米,摩托罗拉和Stock android版本上对此进行了测试,即使禁用了通知,小米(MiUi)也会显示吐司。

解决方案是避免使用Toast来获取重要信息,而应使用Snackbar。但这可能会花费您时间和金钱。