Android 怎么获取 INJECT_EVENTS(小米手机)

本文介绍了一个基于UiAutomator2实现的自动登录示例遇到的点击无权限问题及解决方案。作者通过调整手机的USB调试设置成功解决了该问题。

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

基于UiAutomator2写了一段自动登录的实列,用真机测试的时候,自动化输入是ok的,但在执行click()点击事件的时候,总是提示无权限,提示异常如下:

Injecting to another application requires INJECT_EVENTS permission


在网上能搜索到的解决方案都试过了,都没有解决无权限的问题,在绝望的时候开始怀疑系统了,我的手机是小米5 MI8的系统,最后自己琢磨终于解决了问题。

解决方案:打开USB调试(安全设置) 再调试,终于解决了点击无权限的问题。


设置 --》 更多设置 --》 开发者选项 --》USB调试(安全设置)


PS:我用的是小米手机,可能只是小米手机会出现这个问题,其他机型我没有试过。

uiautomator2.exceptions.RPCUnknownError: ('Unknown RPC error: -32001 java.lang.SecurityException', (1117.5, 248.5), 'java.lang.SecurityException: Injecting input events requires the caller (or the source of the instrumentation, if any) to have the INJECT_EVENTS permission.\n\tat android.os.Parcel.createExceptionOrNull(Parcel.java:3256)\n\tat android.os.Parcel.createException(Parcel.java:3240)\n\tat android.os.Parcel.readException(Parcel.java:3223)\n\tat android.os.Parcel.readException(Parcel.java:3165)\n\tat android.hardware.input.IInputManager$Stub$Proxy.injectInputEventToTarget(IInputManager.java:1542)\n\tat android.hardware.input.InputManagerGlobal.injectInputEvent(InputManagerGlobal.java:1388)\n\tat android.hardware.input.InputManagerGlobal.injectInputEvent(InputManagerGlobal.java:1398)\n\tat android.app.UiAutomationConnection.injectInputEvent(UiAutomationConnection.java:166)\n\tat android.app.UiAutomation.injectInputEvent(UiAutomation.java:991)\n\tat android.app.UiAutomation.injectInputEvent(UiAutomation.java:964)\n\tat com.wetest.uia2.stub.TouchController.injectEventSync(TouchController.java:38)\n\tat com.wetest.uia2.stub.TouchController.touchDown(TouchController.java:47)\n\tat com.wetest.uia2.stub.AutomatorServiceImpl.click(AutomatorServiceImpl.java:191)\n\tat java.lang.reflect.Method.invoke(Native Method)\n\tat com.googlecode.jsonrpc4j.JsonRpcBasicServer.invoke(JsonRpcBasicServer.java:467)\n\tat com.googlecode.jsonrpc4j.JsonRpcBasicServer.handleObject(JsonRpcBasicServer.java:352)\n\tat com.googlecode.jsonrpc4j.JsonRpcBasicServer.handleJsonNodeRequest(JsonRpcBasicServer.java:283)\n\tat com.googlecode.jsonrpc4j.JsonRpcBasicServer.handleRequest(JsonRpcBasicServer.java:251)\n\tat com.wetest.uia2.stub.AutomatorHttpServer.serve(AutomatorHttpServer.java:101)\n\tat fi.iki.elonen.NanoHTTPD.serve(NanoHTTPD.java:2244)\n\tat fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:945)\n\tat fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)\n\tat java.lang.Thread.run(Thread.java:1012)\nCaused by: android.os.RemoteException: Remote stack trace:\n\tat com.android.server.input.InputManagerService.injectInputEventToTarget(InputManagerService.java:929)\n\tat android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:727)\n\tat android.os.Binder.execTransactInternal(Binder.java:1507)\n\tat android.os.Binder.execTransact(Binder.java:1451)\n\n')
最新发布
06-25
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值