Android: Service中创建窗口显示

WindowManager.LayoutParams:

int TYPE_SYSTEM_ALERT  Window type: system window, such as low power alert.(系统窗体,例如低电量警告提示框)
int TYPE_SYSTEM_OVERLAY  Window type: system overlay windows, which need to be displayed on top of everything else.(系统覆盖窗体,哪个需要显示在最前的)

TYPE_SYSTEM_ALERT跟TYPE_SYSTEM_OVERLAY的区别

system_alert窗口可以获得焦点,响应操作

system_overlay窗口显示的时候焦点在后面的Activity上,仍旧可以操作后面的Activity

清单文件manifest中需要相应的permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

以下两种方式显示出来的窗口都不会激发当前Activity的onPause()事件。

1. Seivece 创建窗体

 private void showSystemDialog() {
/* create ui dialog */
View v = View.inflate(mContext, R.layout.serveice_dialog, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setView(v);
dialog = alertDialog.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
dialog.show();
**** 注意dialog 在执行show方法之后,才能调整dialog的大小以及更新数据 ****
/* set size & pos */
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (display.getHeight() > display.getWidth()) {
//lp.height = (int) (display.getHeight() * 0.5);
lp.width = (int) (display.getWidth() * 1.0);
} else {
//lp.height = (int) (display.getHeight() * 0.75);
lp.width = (int) (display.getWidth() * 0.5);
}
d.getWindow().setAttributes(lp); /* update ui data */
lv = (ListView) d.getWindow().findViewById(R.id.listview);
SimpleAdapter adapter = new SimpleAdapter(mContext, getListData(), R.layout.list_item,
new String[]{"item_text", "item_img"},
new int[]{R.id.item_text, R.id.item_img});
lv.setAdapter(adapter); /* set listener */
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
d.dismiss();
}
}); }

2. 直接使用WindowManager.addView()在后台直接显示窗口

  LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDesktopLayout = inflater.inflate(R.layout.volume_panel, null);
// 取得系统窗体
mWindowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
// 窗体的布局样式
mLayoutParams = new WindowManager.LayoutParams();
// 设置窗体显示类型――TYPE_SYSTEM_ALERT(系统提示)
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// 设置窗体焦点及触摸:
// FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 设置显示的模式
mLayoutParams.format = PixelFormat.RGBA_8888;
// 设置对齐的方法
mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
// 设置窗体宽度和高度
mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 设置窗体显示的位置,否则在屏幕中心显示
mLayoutParams.x = 50;
mLayoutParams.y = 50;
mWindowManager.addView(mDesktopLayout, mLayoutParams);
上一篇:PLSql 连接 Oracle


下一篇:mysql 的root 用户无法授权,navicat 远程授权提示1044解决方案