SYSTEM_ALERT_WINDOW
有几个问题设备进入休眠状态(屏幕锁定或屏幕关闭)后,看不到我的系统警报窗口
有时无法点击按钮closeWindow()但不明白为什么。也许有人可以解释一下?
Android 6不再显示此窗口?来自Android设备的任何反应。
我的代码示例
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mContext = this.getApplicationContext();
String phone = intent.getStringExtra("phone").replace("+", "");
Log.d(LOG_TAG, "WindowService::showWindow()");
try {
Map<String, String> map = new HashMap<>();
FCINetwork fciNetwork = new FCINetwork(mContext);
if (fciNetwork.isNetworkAvailable()) {
ArrayList<String> obj = new RequestTask(mContext).execute(phone).get();
if (obj.get(0).indexOf("0") != -1) {
map.put("phone_number", phone);
map.put("personal_data", mContext.getString(R.string.unknown_caller));
map.put("company", "");
map.put("position", "");
} else {
map.put("phone_number", phone);
map.put("personal_data", obj.get(1) + " " + obj.get(2));
map.put("company", obj.get(3));
map.put("position", "");
showWindow(mContext, map);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "Service start");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "Service stop");
Log.d(LOG_TAG, "WindowsService::closeWindow()");
closeWindow();
}
private void showWindow(Context context, Map<String, String> data) {
windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP;
viewGroup = (ViewGroup) layoutInflater.inflate(R.layout.info, null);
TextView phoneNumber = (TextView) viewGroup.findViewById(R.id.phone_number);
phoneNumber.setText(data.get("phone_number"));
TextView personalData = (TextView) viewGroup.findViewById(R.id.personal_data);
personalData.setText(data.get("personal_data"));
TextView company = (TextView) viewGroup.findViewById(R.id.company);
company.setText(data.get("company"));
TextView position = (TextView) viewGroup.findViewById(R.id.position);
position.setText(data.get("position"));
Button buttonClose = (Button) viewGroup.findViewById(R.id.button_close);
buttonClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeWindow();
}
});
windowManager.addView(viewGroup, params);
Log.d("SERVICE", "WORKING");
}
private void closeWindow() {
if (viewGroup != null && windowManager != null) {
windowManager.removeView(viewGroup);
viewGroup = null;
}
}
感谢您的帮助
答案 0 :(得分:1)
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
,
PixelFormat.TRANSLUCENT
);