Android 安卓:覆盖按钮onClick永远不会被调用
我已经创建了一个运行服务的应用程序。该服务基本上有一个覆盖按钮,点击按钮可以做一些事情。但是,每次我单击按钮时,按钮背景中的应用程序都会响应。有人能帮忙吗 我提到过 这是我的密码: 服务代码:Android 安卓:覆盖按钮onClick永远不会被调用,android,service,touch,Android,Service,Touch,我已经创建了一个运行服务的应用程序。该服务基本上有一个覆盖按钮,点击按钮可以做一些事情。但是,每次我单击按钮时,按钮背景中的应用程序都会响应。有人能帮忙吗 我提到过 这是我的密码: 服务代码: public class HUD extends Service implements OnTouchListener{ Button mButton; @Override public IBinder onBind(Intent intent) { // TODO
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,
现在,无论我点击哪里,触摸事件都会被调用。有人能帮我吗?问题是您正在使用WindowManager.LayoutParams.TYPE\u SYSTEM\u OVERLAY。我认为,由于一些安卓4.x,它不再可用。这是我实现叠加按钮的方式:
public class OverlayButton extends Service implements OnTouchListener {
Button mButton;
WindowManager wm;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setTextColor(Color.BLACK);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(150,
150, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.CENTER;
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) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("OverlayButton onTouch", "touched the button");
stopSelf();
}
return true;
}
}
我所做的是,我也给了他再次消失的能力,如果我碰他
这就是我让他出现的方式:
getActivity().startService(new Intent(getActivity().getBaseContext(), OverlayButton.class));
但是,如果您从活动开始,只需使用:
startService(new Intent(this, OverlayButton.class));
别忘了在您的清单中设置这些:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
在标签内,也不要忘记:
<service android:name="OverlayButton" ></service>
当您使用此
WindowManager.LayoutParams.FLAG\u WATCH\u OUTSIDE\u TOUCH
标志时,它告诉应用程序注意您视图之外的所有触摸
尝试改用WindowManager.LayoutParams.FLAG\u NOT\u TOUCH\u MODAL
。这将只截取您视图上的接触
要确认,您需要使用
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
使视图可点击。在onTouch中返回false将告诉应用程序继续处理触摸,就像按钮未被触摸一样。尝试返回true。