【问题标题】:How to detect MotionEvent.ACTION_DOWN in service Android如何在服务 Android 中检测 MotionEvent.ACTION_DOWN
【发布时间】:2017-03-06 01:25:29
【问题描述】:

我正在运行后台服务来检测 Android 5.0 中的 MotionEvent.ACTION_DOWN。我使用了下面的代码,它可以检测到我的触摸事件,但我无法触摸其他应用程序。我该如何解决?如果你有更好的解决方案,请给我。谢谢大家。

public class TouchServiceListener extends Service implements OnTouchListener {
    private WindowManager mWindowManager;
    // linear layout will use to detect touch event
    private LinearLayout touchLayout;   

     @Override
    public void onCreate() {
        super.onCreate();
        touchLayout = new LinearLayout(this);
            // set layout width 30 px and height is equal to full screen
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            touchLayout.setLayoutParams(lp);
            // set color if you want layout visible on screen
            touchLayout.setBackgroundColor(Color.CYAN);
            // set on touch listener
            touchLayout.setOnTouchListener(this);

            // fetch window manager object
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            // set layout parameter of window manager
            WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT, // width of layout 30 px
                    WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
                    WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus
                    PixelFormat.TRANSLUCENT);
            mParams.gravity = Gravity.LEFT | Gravity.TOP;
            Log.i(TAG, "add View");

            mWindowManager.addView(touchLayout, mParams);
    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {        
        if(motionEvent.getAction() == MotionEvent.ACTION_UP|motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d(TAG, "Touch me");
        }
        return true;
    }
 @Override
 public IBinder onBind(Intent intent) {
    return null;
  }
 }

清单

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

【问题讨论】:

    标签: android android-widget android-service


    【解决方案1】:

    首先根据overlay.you设置宽度和高度match_parent覆盖整个屏幕,这样您就不会访问应用程序之外的应用程序。

     package anew.mikka.myapplication;
    
        import android.app.Service;
        import android.content.Intent;
        import android.graphics.PixelFormat;
        import android.os.IBinder;
        import android.view.Gravity;
        import android.view.MotionEvent;
        import android.view.View;
        import android.view.WindowManager;
        import android.view.View.OnTouchListener;
        import android.widget.Button;
        import android.widget.Toast;
    
        public class max extends Service implements View.OnTouchListener {
            Button mButton;
            @Override
            public IBinder
            onBind(Intent intent) {
                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_ALERT,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                        PixelFormat.TRANSLUCENT);
                params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
                params.setTitle("Load Average");
                WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
                wm.addView(mButton, params);
    
            }
    
            @Override
            public void onDestroy() {
                super.onDestroy();
                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;
            }
    
    
        }
    

    【讨论】:

    • 我之前试过。但是当我使用上面的代码时,它不能按任何应用程序。你能试试吗?
    • 我在这篇文章之后尝试了这段代码。如果你想访问另一个应用程序控件,那么它是不可能的。
    • 谢谢。我已经测试过了。抱歉耽搁了。您的代码有效。但是,它在我的屏幕上显示了一个按钮。我试图让它不可见,但是当我让它不可见时触摸不会显示。
    • 我修好了,我设置了mButton = new Button(this); mButton.setLayoutParams(new LinearLayout.LayoutParams(1, 1));WindowManager.LayoutParams params = new WindowManager.LayoutParams( 1, 1,
    • 如何处理onTouch()内部的触摸类型
    猜你喜欢
    • 2013-09-23
    • 2017-08-27
    • 1970-01-01
    • 2013-01-16
    • 2020-11-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多