记录在安卓应用中,应用在后台是弹出对话框或显示一个view在前台界面上相关方法以及参数,效果。
int windowType
对话框的设置
final AlertDialog dialog = new AlertDialog.Builder(this).setMessage("test dialog").create();
dialog.getWindow().setType(windowType);
final WindowManager.LayoutParams alertParams = new WindowManager.LayoutParams();
alertParams.type = windowType;
alertParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
alertParams.width = WindowManager.LayoutParams.MATCH_PARENT;
alertParams.height = 300;
alertParams.format = PixelFormat.RGBA_8888;
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
final TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.YELLOW);
view.setText("test");
wm.addView(view, alertParams);//显示view
实现方式是这样子的:一共2个Activity,1个Service
MainActivity onCreate中的代码:
final AlertDialog dialog = new AlertDialog.Builder(getApplicationContext()).setMessage("test activity1 dialog").create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
final WindowManager.LayoutParams alertParams = new WindowManager.LayoutParams();
alertParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
alertParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
alertParams.width = WindowManager.LayoutParams.MATCH_PARENT;
alertParams.height = 300;
alertParams.format = PixelFormat.RGBA_8888;
final WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
final TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.BLUE);
view.setText("test");
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(v);
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
wm.addView(view, alertParams);
dialog.show();
}
}, 5000);
startService(new Intent(this, Test1Service.class));
startActivity(new Intent(this,Test2Activity.class));
final AlertDialog dialog = new AlertDialog.Builder(this).setMessage("test service dialog").create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
final WindowManager.LayoutParams alertParams = new WindowManager.LayoutParams();
alertParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
alertParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
alertParams.width = WindowManager.LayoutParams.MATCH_PARENT;
alertParams.height = 300;
alertParams.format = PixelFormat.RGBA_8888;
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
final TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.YELLOW);
view.setText("test");
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(v);
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
wm.addView(view, alertParams);
dialog.show();
stopSelf();
}
}, 3000);
final AlertDialog dialog = new AlertDialog.Builder(this).setMessage("test activity2 dialog").create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
final WindowManager.LayoutParams alertParams = new WindowManager.LayoutParams();
alertParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
alertParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
alertParams.width = WindowManager.LayoutParams.MATCH_PARENT;
alertParams.height = 300;
alertParams.format = PixelFormat.RGBA_8888;
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
final TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.GREEN);
view.setText("test2");
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(v);
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i(getPackageName(), "显示 activity2");
wm.addView(view, alertParams);
dialog.show();
}
}, 7000);
注意黄色背景的地方,有关键的影响。
根据顺序,首先是service的显示,然后MainActivity,最后Test2Activity。
以下所述显示情况为启动后立即返回桌面后的显示情况
1、windowType = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT || WindowManager.LayoutParams.TYPE_SYSTEM_ERROR || WindowManager.LayoutParams.TYPE_TOAST
TYPE_SYSTEM_ALERT 需要权限<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"/>
service显示,MainActivity显示,Test2Activity未显示(在4.4以下版本机型上,出现界面显示异常,不知道是不是在虚拟机中的原因,没有真机测试。。。另外,在5.0以上的机型,Test2Activty的对话框和自定义view都显示了)
根据情况来看,是否在后台能弹出显示,关键就是代码中黄色部分所用的是本身的context还是applicationContext(此处说明下,service不管是用本身还是applicationContext,均能弹出显示),这个解释应该就是应用的窗口和单独activity的窗口关系。
TYPE_SYSTEM_ERROR 会遮罩整个屏幕,包括虚拟的“返回”,“HOME"按键
2、windowType = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
该类型在显示后,用户无法点击到显示的对话框以及自定义view,但能点击到所覆盖的控件,即触摸事件穿透过去了
关于应用窗口和activity窗口的关联,有待研究,后面补充几个type的说明