我是在Android Studio上编写测试的新手。通过使用UiAutomator编写仪器测试,我完成了一些自动测试。但是,当我需要获得设备IMEI和服务状态(需要权限READ_PHONE_STATE)时,即使在清单和代码中添加了权限,也总是不允许执行SecurityException。
检查Android文档,我知道权限READ_PHONE_STATE处于危险级别,因此我需要在运行时从用户那里获得权限。但是问题是我的测试代码不包含Activity。我在网上做了几天的研究,包括在Stack Overflow中找出可能的解决方案,但是没有运气。
我尝试过的包括:
1。使用@Rule
@Rule
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.READ_PHONE_STATE);
2。使用shell命令:
myDevice.executeShellCommand("pm grant com.mp.rss android.permission.READ_PHONE_STATE");
3。使用PermissionRequester(我可以看到在Logcat中授予的权限,但仍然收到SecurityException)
PermissionRequester pr = new PermissionRequester();
pr.addPermissions("android.permission.READ_PHONE_STATE");
pr.requestPermissions();
在logcat中:I / GrantPermissionCallable:权限:android.permission.READ_PHONE_STATE已被授予!
4。尝试提示询问权限,但失败,因为我的测试程序中没有活动。
int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 100;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && context.checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE )){
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
这是我的清单。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mp.rss">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RREAD_PRIVILEGED_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBER"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" />
</manifest>
这是我的代码
public class rss {
private static UiDevice myDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
private static Context context = InstrumentationRegistry.getInstrumentation().getContext();
public static void writeDeviceinfo( ) throws IOException, NullPointerException, UiObjectNotFoundException {
PermissionRequester pr = new PermissionRequester();
pr.addPermissions("android.permission.READ_PHONE_STATE");
pr.requestPermissions();
try {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
writeLog("[PrintInfo] Device service is = " + tMgr.getServiceState());
}catch (NullPointerException e) {
writeLog("[PrintInfo] Device service is = ");
}
}
}
这是我尝试的以上4种方法的异常消息
`java.lang.SecurityException: com.mp.rss.test from uid 10181 not allowed to perform OP_READ_PHONE_STATE
at android.os.Parcel.createException(Parcel.java:1946)
at android.os.Parcel.readException(Parcel.java:1914)
at android.os.Parcel.readException(Parcel.java:1864)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getServiceStateForSubscriber(ITelephony.java:5590)
at android.telephony.TelephonyManager.getServiceStateForSubscriber(TelephonyManager.java:7576)
at android.telephony.TelephonyManager.getServiceState(TelephonyManager.java:7564)
at .....`