View view = View.inflate(StatusBarService.this, R.layout.power_off_dialog, null);
AlertDialog.Builder b = new AlertDialog.Builder(StatusBarService.this);
b.setView(view);
final AlertDialog d = b.create();
d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); //系统中关机对话框就是这个属性
//d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); //窗口可以获得焦点,响应操作
//d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY); //窗口不可以获得焦点,点击时响应窗口后面的界面点击事件
d.show();
Button yesButton = (Button) view.findViewById(R.id.yes_button);
Button canclButton = (Button) view.findViewById(R.id.cancl_button);
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
canclButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
最后记得加权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />