have several problems with SYSTEM_ALERT_WINDOW
After device go sleep (Screen lock or screen switch off) don't see my System alert window
Sometimes can't click button closeWindow() but don't understand why. Maybe some one can explain?
Android 6 no more show this window? Any reaction from android device.
My code example
@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;
}
}
Thank you for your help