android framework InputManager模拟触摸事件inject详解实战开发-视频课程笔记

93 篇文章 58 订阅
90 篇文章 66 订阅

我相信触摸事件注入,即模拟触摸事件绝对是最受我们android开发者喜欢的,想想你程序可以自由的模拟出你的点击事件了,你就完全可以不用在手动了,可以帮你干各种各样的事。
但是同学们可能对模拟触摸事件使用及其原理其实并不是非常了解,大部分都可能是基于使用级别的居多,至于使用过程中可能出现的问题,及模拟触摸事件本质上在android系统中的一个原理,其实大部分都不是很清楚,所以本节课就来带大家去深入了解这个模拟触摸事件相关的,这个blog当然只是课程的部分课件截图,需要更清楚认真可以看我的相关视频

新课程优惠获取请加入qq群:422901085
Android手机大厂Framework系统-Input系统专题实战课
https://ke.qq.com/course/4963459

[入门课,实战课,跨进程专题
ps需要学习深入framework课程和课程优惠
课程概要:
在这里插入图片描述
InjectInput的源码分析,以下是他的一个流程图,一直从应用端binder调用到了了SystemServer然后再调用到了InputDispatcher,即注入的事件其实是完全没有经过InputReader的,直接就到达了InputDispatcher端进行了分发过程:
在这里插入图片描述
了解我们的触摸事件的注入原理后,我们来看看我们应用端是需要怎么样来实现对事件的注入呢?因为InputManager对应的注入分发其实是隐藏方法,即不是sdk公开普通api,那么就需要通过反射调用InputManager,或者调用Instrumentation对应方法,以及对事件注入也是有对应权限问题,因为你想想,不可能让你一个普通第三方应用可以随意注入触摸事件影响其他的应用的窗口,必现要对这个注入事件进行一个权限保护,如果没有权限,只能注入自己进程的相关window
在这里插入图片描述

实战代码:

package com.example.injectmotion;

import android.app.Instrumentation;
import android.hardware.input.InputManager;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    //    .setAction("Action", null).show();
               // injectClickSelf();
                injectClickOther();
            }
        });
       //InputManager inputManager =  (InputManager) getSystemService(INPUT_SERVICE)
        //inputManager.inj

    }
    void injectClickSelf() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Instrumentation instrumentation = new Instrumentation();
                final long now = SystemClock.uptimeMillis();
                int action = MotionEvent.ACTION_DOWN;
                float x = 583;
                float y = 1350;
                MotionEvent clickDown = MotionEvent.obtain(now,now,action,x,y,0);
                instrumentation.sendPointerSync(clickDown);
                action = MotionEvent.ACTION_UP;
                MotionEvent clickUp = MotionEvent.obtain(now,now,action,x,y,0);
                instrumentation.sendPointerSync(clickUp);
            }
        }).start();
    }
    void injectClickOther() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i= 0;i < 50 ;i++) {
                    Instrumentation instrumentation = new Instrumentation();
                    final long now = SystemClock.uptimeMillis();
                    int action = MotionEvent.ACTION_DOWN;
                    float x = 583;
                    float y = 1350;
                    MotionEvent clickDown = MotionEvent.obtain(now,now,action,x,y,0);
                    instrumentation.sendPointerSync(clickDown);
                    action = MotionEvent.ACTION_UP;
                    MotionEvent clickUp = MotionEvent.obtain(now,now,action,x,y,0);
                    instrumentation.sendPointerSync(clickUp);
                    try {
                        Thread.sleep(500);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("test"," onTouchEvent event = " + event);
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

运行效果,点击按钮后触发,会有一个模拟点击点:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值