检测覆盖中的触摸事件并将其进一步传递 [英] Detecting touch events in overlay and passing them further

查看:91
本文介绍了检测覆盖中的触摸事件并将其进一步传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个overlay手势检测器,该手势检测器将显示在所有其他应用程序上方. 叠加层将从Service开始.

I want to make an overlay gesture detector that will be displayed above all other apps. The overlay will be started from a Service.

我提供了示例服务,只是为了检查是否可以以某种方式记录来自触摸覆盖的图标的触摸事件,并将其进一步分发给当前处于活动状态的应用程序:

I've made sample service just to check if touch event from touching overlaid icon can be somehow recorded and dispatched further to currently active app:

class GestureDetectorService : Service() {
override fun onBind(intent: Intent?): IBinder? = null

val windowManager: WindowManager
    get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager

var iv: AppCompatImageView? = null

override fun onCreate() {
    super.onCreate()
    iv = AppCompatImageView(this)
    iv?.setImageResource(R.drawable.icon)

    iv?.setOnTouchListener { view, motionEvent ->
        when (motionEvent.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d("Click", "From icon")
                false
            }
            else -> false
        }
    }

    val params: WindowManager.LayoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT
            , WindowManager.LayoutParams.WRAP_CONTENT
            , WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            or WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
            or WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
            , PixelFormat.TRANSLUCENT)

    params.gravity = Gravity.TOP or Gravity.LEFT
    params.x = 100
    params.y = 100

    windowManager.addView(iv, params)
}

override fun onDestroy() {
    super.onDestroy()
    iv.let { windowManager.removeView(iv) }
}
}

没有运气.

比方说,我想检测用户何时以一定速度执行Fling或在屏幕上绘制"特定形状,从而触发例如记事本的启动.

let's say I want to detect when user will perform a Flingof certain velocity, or 'draw' certain shape on the screen, which would trigger for example launching of notepad.

但是我不希望这一切阻止正常的应用程序交互,用户必须能够与正常启动的任何应用程序进行交互.

But I don't want any of this to block normal app interaction, user must be able to interact with whatever app is launched normally.

用Kotlin给出的简洁答案是非常好的,但是如果您想用Java编写一些东西,那也很棒.

Concise answer in Kotlin would be super nice, but if you will come up with something in Java, that also would be great.

推荐答案

幸运的是,除了可能在有根设备上或通过自定义固件,这是不可能的.

Fortunately, this is not possible, except perhaps on rooted devices or via custom firmware.

您所描述的是一种劫持攻击:监视用户输入,同时传递相同的输入,以产生正常的效果.自Android 4.0以来,出于隐私和安全原因已将其阻止.

What you are describing is a tapjacking attack: spying on user input while passing that same input along so it has normal effects. This has been blocked for privacy and security reasons since Android 4.0.

这篇关于检测覆盖中的触摸事件并将其进一步传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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