在Android Service中弹出系统全屏对话框
最终的效果图
布局文件就不贴了,这里只写出关键的代码。
1、自定义对话框
Window window = mAlertDialog.getWindow();
//关键代码,设置成ALERT,这样点击back键也无法让对话框消失 window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); mAlertDialog.show();
//以下代码看需求添加,要在show之后调用,被对话框遮挡的半透明区域 window.setBackgroundDrawableResource(android.R.color.transparent); WindowManager.LayoutParams layoutParams = window.getAttributes(); layoutParams.gravity=Gravity.TOP; layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height= WindowManager.LayoutParams.MATCH_PARENT; window.getDecorView().setPadding(0, 0, 0, 0); window.setAttributes(layoutParams); window.setContentView(R.layout.xxxxx);
2、在Service中使用
如果要在service使用,直接调用即可,如果是在service的线程中显示对话框,因为自行创建的线程没有Looper,因此需要添加代码如下:
//线程池回调回来的,没有looper会显示不了界面 Looper.prepare(); popupUpgradeTips(); Looper.loop();