- 項目中為了實現(xiàn)賬號多設(shè)備登錄的監(jiān)聽 一個賬號在別的設(shè)備登錄時在該設(shè)備上需要彈出對話框提示 故而用到全局對話框
實現(xiàn)方式
- [ 1 ]. 添加權(quán)限
<!--全局彈窗的權(quán)限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
- [ 2 ]. 設(shè)置dialog類型
一定要在show之前設(shè)置
如果類型為TYPE_SYSTEM_ALERT需要開啟懸浮窗權(quán)限 使用相當(dāng)不方便
因此使用TYPE_TOAST 一般設(shè)備不需要特意去開啟權(quán)限即可使用
//設(shè)置彈出全局對話框
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();
//設(shè)置彈出全局對話框
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實現(xiàn)全局對話框
/**
* 顯示彈出框
*
* @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;
// 設(shè)置flag
params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果設(shè)置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE在抛,彈出的View收不到Back鍵的事件
// 不設(shè)置這個彈出框的透明遮罩顯示為黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口
// 設(shè)置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時进栽,后面的應(yīng)用圖標(biāo)由不可長按變?yōu)榭砷L按
// 不設(shè)置這個flag的話义桂,home頁的劃屏?xí)袉栴}
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("該賬戶在其他設(shè)備登錄,若不是您在操作,請及時修改密碼以防泄露信息");
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);
}