Android:onClick按钮onClick永远不会被调用

时间:2013-07-23 13:59:10

标签: android service touch

我创建了一个运行服务的应用程序。该服务基本上有一个叠加按钮,点击按钮做了一些事情。但是,每次我点击按钮,按钮背景中的应用程序都会响应。请有人帮忙吗?

我已提到link

这是我的代码:

服务代码:

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,

现在,无论我点击哪里,都会调用触摸事件。请有人帮帮我吗?

2 个答案:

答案 0 :(得分:6)

问题是您正在使用WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY。我认为,因为一些Android 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));

但是,如果你从Activity中启动它,只需使用:

startService(new Intent(this, OverlayButton.class));

不要忘记在清单中设置这些:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

在标签内部也不要忘记:

<service android:name="OverlayButton" ></service>

答案 1 :(得分:1)

当您使用此WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH标记时,它会告诉应用监视您视图之外的所有内容。

请尝试使用WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL。这将仅截取您的观点。

要确认,您需要使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT来使视图可点击。