Android:自动点击屏幕

本文介绍了如何使用ADB Shell进行自动测试,包括模拟滑动、单击、长按等操作。并通过示例代码展示了如何在Android应用中实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发的过程中会遇到自动测试功能,比如如何自动点击按钮进行测试,当然可以使用按键精灵等工具进行测试,不过如何在程序进程中测试呢,下面就介绍下adb shell的操作,通过shell进行点击等操作。

1.模拟滑动

input swipe startX startY endX endY duration(ms) 

2.单击某点

input tap x y

3.长按某点

input touchscreen swipe x y x y duration(ms)

4.单击某个键

input keyevent keyCode

5.长按某个键

input keyevent --longpress keyCode

一、如下以点击为例,传入当期点所在屏幕位置或者比例

package cn.test.autotouch;

import android.app.Activity;
import java.io.IOException;

public class AutoTouch {
    public int width = 0;
    public int height = 0;

    /**
     * 传入在屏幕中的比例位置,坐标左上角为基准
     * @param act 传入Activity对象
     * @param ratioX 需要点击的x坐标在屏幕中的比例位置
     * @param ratioY 需要点击的y坐标在屏幕中的比例位置
     */
    public void autoClickRatio(Activity act, final double ratioX, final double ratioY) {
        width = act.getWindowManager().getDefaultDisplay().getWidth();
        height = act.getWindowManager().getDefaultDisplay().getHeight();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 线程睡眠0.3s
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 生成点击坐标
                int x = (int) (width * ratioX);
                int y = (int) (height * ratioY);

                // 利用ProcessBuilder执行shell命令
                String[] order = { "input", "tap", "" + x, "" + y };
                try {
                    new ProcessBuilder(order).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 传入在屏幕中的坐标,坐标左上角为基准
     * @param act 传入Activity对象
     * @param x 需要点击的x坐标
     * @param y 需要点击的x坐标
     */
    public void autoClickPos(Activity act, final double x, final double y) {
        width = act.getWindowManager().getDefaultDisplay().getWidth();
        height = act.getWindowManager().getDefaultDisplay().getHeight();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 线程睡眠0.3s
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 利用ProcessBuilder执行shell命令
                String[] order = { "input", "tap", "" + x, "" + y };
                try {
                    new ProcessBuilder(order).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

二、使用方法

    /***************************定义*****************************/
    //声明一个Activity
    public static Activity staticActivity;
    //初始化AutoTouch对象
    public static AutoTouch autoTouch = new AutoTouch();
    //在onCreate中对staticActivity赋值
    staticActivity = this;

    /***************************使用*****************************/
    //传入所在比例
    autoTouch.autoClickRatio(staticActivity, 0.4375, 0.537); 
    //出入坐标
    autoTouch.autoClickPos(staticActivity, 840, 580);
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值