1.设置dialog为系统dialog
AlertDialog dialog = new AlertDialog.Builder(mContext)
.setTitle("dd")
.setMessage("fhjk")
.setPositiveButton("quer",null)
.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
2.监听home键按下:Intent.ACTION_CLOSE_SYSTEM_DIALOGS
public class CustomDialog extends Dialog {
private CloseSystemDialogsReceiver mCloseSystemDialogsReceiver;
private Window mWindow;
public CustomDialog(Context context) {
super(context);
setContentView(R.layout.view_custom);
mWindow = this.getWindow();
LayoutParams attributes = mWindow.getAttributes();
attributes.width = mWindow.getWindowManager().getDefaultDisplay()
.getWidth();
attributes.height = LayoutParams.MATCH_PARENT;
mWindow.setType(LayoutParams.TYPE_SYSTEM_ALERT);
IntentFilter filter = new IntentFilter(
Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mCloseSystemDialogsReceiver = new CloseSystemDialogsReceiver();
mWindow.getContext().registerReceiver(mCloseSystemDialogsReceiver,
filter);
}
private class CloseSystemDialogsReceiver extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
CustomSymbolDialog.this.dismiss();
mWindow.getContext().unregisterReceiver(mCloseSystemDialogsReceiver);
}
}
}
}
}
3.dialog 按下power键不重新显示 遮挡UI 解决方法 重新:dismiss和show
class WakeAndLockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
AppLockPreferenceController.alertDialog.show();
}if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
AppLockPreferenceController.alertDialog.dismiss();
}
}
}
4.dialog 过大,一页显示不下
使用 NestedScrollView包裹动态设置
dialog中弹出软键盘总是把布局顶到顶部挤压,这时候需要把根布局设置成
NestedScrollView 包裹动态设置
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);