1.通过创建Instrumentation对象来实现,该类中有一个这样的方法onKeyDown(KeyEvent.KEYKODE_BACK,null)
2.在AndroidManifest.xml中添加INJECT_EVENTS权限。
<users-permission android:name="android.permission.INJECT_EVENTS"/>
3.如果还是会报出权限问题,那么就需要为app添加sharedUserId属性,不过添加该属性存在很大风险,因为添加之后,该app就会分享其他系统应用的数据。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.****.***" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system">
另外,关于sharedUserId官方解释如下:
The nameof a Linux user ID that will be shared with other applications.
By default,Android assigns each application its own unique user ID.
However, if thisattribute is set to the same value for two or more applications,
they will allshare the same ID — provided that they are also signed by the
same certificate.Application with the same user ID
can access each other's data and, if desired,run in the same process.