Android keyguard之上如何显示Toast

来源:互联网 发布:java构造方法不写void 编辑:程序博客网 时间:2024/05/31 15:19

ENV : Android M 6.0.1


锁屏之上应该如何显示Toast呢? 看下面的实现:

public class KeyguardToast {    public static Toast makeText(Context context, CharSequence text, int duration) {        Toast toast = Toast.makeText(context, text, duration);        toast.getWindowParams().type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;        toast.getWindowParams().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;        toast.getWindowParams().flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;        // set offset position        toast.setGravity(Gravity.CENTER, 0, 400);        return toast;    }}


锁屏之上如何显示Dialog?看下面的实现:

public class SystemUIDialog extends AlertDialog {    private final Context mContext;    public SystemUIDialog(Context context) {        super(context, R.style.Theme_SystemUI_Dialog);        mContext = context;        getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);        WindowManager.LayoutParams attrs = getWindow().getAttributes();        attrs.setTitle(getClass().getSimpleName());        getWindow().setAttributes(attrs);    }    public void setShowForAllUsers(boolean show) {        if (show) {            getWindow().getAttributes().privateFlags |=                    WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;        } else {            getWindow().getAttributes().privateFlags &=                    ~WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;        }    }    public void setMessage(int resId) {        setMessage(mContext.getString(resId));    }    public void setPositiveButton(int resId, OnClickListener onClick) {        setButton(BUTTON_POSITIVE, mContext.getString(resId), onClick);    }    public void setNegativeButton(int resId, OnClickListener onClick) {        setButton(BUTTON_NEGATIVE, mContext.getString(resId), onClick);    }}



0 0