Android的 - 使用SYSTEM_OVERLAY改变触摸事件 [英] Android - Use SYSTEM_OVERLAY to change touch events

查看:315
本文介绍了Android的 - 使用SYSTEM_OVERLAY改变触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是pretty很多类似<一个href=\"http://stackoverflow.com/questions/9085022/android-overlay-to-grab-all-touch-and-pass-them-on\">this 之一。
这个问题几乎是3年前问,并没有回答。

我的目标是赶上使用系统覆盖所有的触摸事件(见code以下),改变事件的参数(即event.x = event.getX()+ 5),并把它传递给当前正在运行的应用程序(任何应用程序)。

目前,我能赶上所有MotionEvent的使用TYPE_PHONE标志或使用TYPE_SYSTEM_OVERLAY,所有事件传递到当前应用程序而不是两个。

该应用程序是用于辅助目的,因此使用任何辅助能力也是一种选择。

基本code的覆盖服务:

 进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.PixelFormat;
进口android.os.IBinder;
进口android.view.Gravity;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.WindowManager;
进口android.widget.Toast;
公共类OverlayService延伸服务{
    OverlayView的oView;    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }    @覆盖
    公共无效的onCreate(){
        super.onCreate();        oView =新OverlayView的(本);
        WindowManager.LayoutParams PARAMS =新WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.TYPE_PHONE,// TYPE_SYSTEM_OVERLAY
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
              | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
              | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);        params.gravity = Gravity.CENTER;
        窗口管理WM =(窗口管理器)getSystemService(WINDOW_SERVICE);
        wm.addView(oView,则params);
    }    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        如果(oView!= NULL)
        {
            ((窗口管理器)getSystemService(WINDOW_SERVICE))removeView(oView)。
            oView = NULL;
        }
    }}类OverlayView的扩展视图{
    私人涂料mLoadPaint;    公共OverlayView的(上下文的背景下){
        超级(上下文);
        setBackgroundColor(Color.BLUE);
        。的getBackground()setAlpha(128);
    }
    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
        如果(event.getAction()== MotionEvent.ACTION_DOWN){
            Toast.makeText(的getContext()的Touch,Toast.LENGTH_SHORT).show();            // **********************这是我需要你的帮助*************
        }
        返回false;
    }
}

任何建议将大大AP preciated。


解决方案

  

我的目标是赶上使用系统覆盖所有的触摸事件(见code以下),改变事件的参数(即event.x = event.getX()+ 5),并把它传递给当前正在运行的应用程序(任何应用程序)。


这是不可能的,隐私和安全方面的原因。


  

目前,我能赶上所有MotionEvent的使用TYPE_PHONE标志或使用TYPE_SYSTEM_OVERLAY所有事件传递到当前应用程序,但不能同时使用。


正确的。那是在Android 4.0的添加修复来防御攻击tapjacking,这就是你正在尝试做的,你是否意识到这一点。


  

该应用程序是用于辅助目的,因此使用任何辅助能力也是一种选择。


我不能排除这种可能性,但我没有看到低级别的触摸事件的 AccessibilityEvent ,更不用说修改它们。这将是从你正在服用,因此有理由进行单独的堆栈溢出问题的做法相当激进的背离。

My question is pretty much similar to this one. The question was asked almost 3 years ago and was not answered yet.

My goal is to catch all touch-events using a system-overlay (see code below), change the event's parameters (i.e. event.x = event.getX()+5) and pass it on to the current running application (any application).

Currently, I can catch all MotionEvent's by using the TYPE_PHONE flag OR pass all of the events to to the current application by using the TYPE_SYSTEM_OVERLAY, but not both.

The application is used for accessibility purpose so using any accessibility ability is also an option.

The basic code for the overlay service:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
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.widget.Toast;


public class OverlayService extends Service {
    OverlayView oView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        oView = new OverlayView(this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.TYPE_PHONE, // TYPE_SYSTEM_OVERLAY
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
              | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
              | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.CENTER;
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(oView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(oView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(oView);
            oView = null;
        }
    }

}

class OverlayView extends View {
    private Paint mLoadPaint;

    public OverlayView(Context context) {
        super(context);
        setBackgroundColor(Color.BLUE);
        getBackground().setAlpha(128);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            Toast.makeText(getContext(), "Touch", Toast.LENGTH_SHORT).show();

            //********************** THIS IS WHERE I NEED YOUR HELP *************
        }
        return false;
    }
}

Any advise will be much appreciated.

解决方案

My goal is to catch all touch-events using a system-overlay (see code below), change the event's parameters (i.e. event.x = event.getX()+5) and pass it on to the current running application (any application).

That is not possible, for privacy and security reasons.

Currently, I can catch all MotionEvent's by using the TYPE_PHONE flag OR pass all of the events to to the current application by using the TYPE_SYSTEM_OVERLAY, but not both.

Correct. That was the fix added in Android 4.0 to defend against tapjacking attacks, which is what you are trying to do whether you realize it or not.

The application is used for accessibility purpose so using any accessibility ability is also an option.

I cannot rule out that possibility, though I don't see low-level touch events in AccessibilityEvent, let alone modifying them. This would be a fairly radical departure from the approach that you are taking and as such would warrant a separate Stack Overflow question.

这篇关于Android的 - 使用SYSTEM_OVERLAY改变触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆