我创建了一个运行服务的应用程序。该服务基本上有一个覆盖按钮,点击按钮可以做一些事情。但是,每次单击按钮时,按钮背景中的应用程序都会响应。请问有人可以帮忙吗?
我提到了链接。
这是我的代码:
服务代码:
public class HUD extends Service implements OnTouchListener{
Button mButton;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
String s;
s = "Hari";
return null;
}
@Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show();
return false;
}
}
清单也具有以下权限:
我尝试将类型更改为
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
现在,无论我点击哪里,都会调用触摸事件。请问有人可以帮我吗?