我创建了一个服务,在每个应用程序的顶部打开一个系统覆盖,一切正常,但是当我触摸外面时它永远不会删除。我搜索了很多但没有找到合适的答案。请帮助我。
代码: -
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
Log.d("inez", "inez onCreate");
super.onCreate();
mContext = getApplicationContext();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSPARENT);
params.gravity = Gravity.CENTER;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.main, null);
SwitchCompat switchCompat = (SwitchCompat) view.findViewById(R.id.notification_switch);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(mContext, "Checked", Toast.LENGTH_LONG).show();
stopService(intent);
}
}
});
wm.addView(view, params);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.e(TAG, "OnDestroy called::");
super.onDestroy();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(view);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}