- 项目中为了实现账号多设备登录的监听 一个账号在别的设备登录时在该设备上需要弹出对话框提示 故而用到全局对话框
实现方式
- [ 1 ]. 添加权限
<!--全局弹窗的权限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
- [ 2 ]. 设置dialog类型
一定要在show之前设置
如果类型为TYPE_SYSTEM_ALERT需要开启悬浮窗权限 使用相当不方便
因此使用TYPE_TOAST 一般设备不需要特意去开启权限即可使用
//设置弹出全局对话框
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
//dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
- [ 3 ]. 完整代码
public static void showDialog(final Context context, final String title, final Boolean isCancelable, final String ok, final String close, final DialogCallback callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
/*true 代表点击空白可消失 false代表点击空白哦不可消失 */
builder.setCancelable(isCancelable);
View view = View.inflate(context, R.layout.item_dialog_exit, null);
TextView tv_itemdialog_title = (TextView) view.findViewById(R.id.tv_itemdialog_title);
TextView tv_itemdialog_ok = (TextView) view.findViewById(R.id.tv_itemdialog_ok);
TextView tv_itemdialog_close = (TextView) view.findViewById(R.id.tv_itemdialog_close);
tv_itemdialog_ok.setText(ok);
tv_itemdialog_close.setText(close);
builder.setView(view);
final AlertDialog dialog = builder.create();
//设置弹出全局对话框
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
dialog.show();
tv_itemdialog_title.setText(title);
tv_itemdialog_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onPositive(dialog);
}
});
tv_itemdialog_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onNegative(dialog);
}
});
使用WindowManager实现全局对话框
/**
* 显示弹出框
*
* @param context
*/
public static void showPopupWindow(final Context context, final DialogCallback callback) {
// 获取WindowManager
final WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 类型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// 设置flag
params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
// 不设置这个弹出框的透明遮罩显示为黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
// 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
// 不设置这个flag的话,home页的划屏会有问题
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
TextView textView = new TextView(context);
textView.setText("sfgsfdsfbsadfbasdfg");
textView.setTextSize(100);
final View mView = LayoutInflater.from(context).inflate(R.layout.item_dialog_exit, null);
TextView tv_itemdialog_title = (TextView) mView.findViewById(R.id.tv_itemdialog_title);
TextView tv_itemdialog_ok = (TextView) mView.findViewById(R.id.tv_itemdialog_ok);
TextView tv_itemdialog_close = (TextView) mView.findViewById(R.id.tv_itemdialog_close);
tv_itemdialog_ok.setText("重新登录");
tv_itemdialog_close.setText("退出登录");
tv_itemdialog_title.setText("该账户在其他设备登录,若不是您在操作,请及时修改密码以防泄露信息");
tv_itemdialog_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 隐藏弹窗
mWindowManager.removeView(mView);
callback.onPositive();
}
});
tv_itemdialog_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWindowManager.removeView(mView);
callback.onNegative();
}
});
mWindowManager.addView(textView, params);
}