您的位置:首页 > 移动开发 > Android开发

Android keyguard之上如何显示Toast

2016-07-08 14:20 393 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: