我创建了一个服务来创建一个检测长按的系统覆盖(按钮),这是我的代码:
public class myservice extends Service implements OnTouchListener {
WindowManager wm;
View myview;
LayoutInflater li;
Button b;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("\n=\n=\n=\n" + "oncreate service" + "\n=\n=\n=\n");
li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
//WindowManager.LayoutParams.TYPE_INPUT_METHOD |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY |
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
//WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
System.out.println("\n=\n=\n=\n" + "lp " + params + "\n=\n=\n=\n");
params.gravity = Gravity.CENTER;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
myview = li.inflate(R.layout.test_layout, null);
System.out.println("\n=\n=\n=\n" + "View inflated" + "\n=\n=\n=\n");
System.out.println("\n=\n=\n=\n" + "Layout inflator = " + li + "\nWindow manager = " + wm + "\nparams = " + params + "\nView = " + myview + "\n=\n=\n=\n");
myview.setOnTouchListener(this);
System.out.println("\n=\n=\n=\n" + "On touch listener" + "\n=\n=\n=\n");
wm.addView(myview, params);
System.out.println("\n=\n=\n=\n" + "add view" + "\n=\n=\n=\n");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
wm.removeView(myview);
myview=null;
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("\n=\n=\n=\n Inside On Touch method \n=\n=\n=\n");
if(event.getAction()==MotionEvent.ACTION_DOWN) {
Toast.makeText(getApplicationContext(), "Press", Toast.LENGTH_SHORT).show();
System.out.println("\n=\n=\n=\ntouch " + event.getX() + " " + event.getY() + "\n=\n=\n=\n");
return true;
}
return false;
}
它应该允许用户正常使用手机,除非用户按下按钮。但我注意到后退按钮不起作用,我手机上的列表视图(如联系人、警报、消息、whatsapp 列表)不再可点击。当我单击列表项时,它显示正常动画,但没有打开任何内容。
我试图找到错误并发现了这一点。
- 当我添加 TYPE_SYSTEM_ALERT 时,问题仍然存在。
- 当我添加 TYPE_SYSTEM_OVERLAY 并且不使用 TYPE_SYSTEM_ALERT 问题消失了,但现在按钮不可点击,就像一个永久图像卡住而没有任何用途(不是我想要的)。
图标工作得非常好,它只是后退按钮和列表视图。为什么 ?我该如何纠正?