private static AlertDialog.Builder builder;
private static AlertDialog alert;
Intent rebootIntent = new Intent("android.intent.action.ACTION_REBOOT");
if(alert != null){
alert.dismiss();
}
// 创建弹窗
builder = new AlertDialog.Builder(mContext);
builder.setMessage("设备即将重启,确定继续吗?")
.setTitle("重启")
//设置点击外部是否关闭弹窗
.setCancelable(true)
.setPositiveButton("重启", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 用户选择重启,发送广播
mContext.sendBroadcast(rebootIntent);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 取消关闭,需要通过反射机制取消关闭
try {
// 反射方法参数为空,无返回值
// Method cancelMethod = intent.getClass().getDeclaredMethod("cancel");
// cancelMethod.setAccessible(true);
// cancelMethod.invoke(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
Android12源码 重启按钮确认重启弹窗
于 2024-12-09 16:19:41 首次发布