Viv*_*vek 8 android android-fragments android-testing android-permissions androidx
我在我的MainActivityTest班级中配置了如下权限规则
@Rule
public GrantPermissionRule permissionRule =
GrantPermissionRule.grant(RECORD_AUDIO, WRITE_EXTERNAL_STORAGE);
Run Code Online (Sandbox Code Playgroud)
当我运行以下命令以使用 api 27 在模拟器上执行测试时
./gradlew connectedCheck
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下错误
com.example.myapplication.MainActivityTest > testLaunch_main_activity[Pixel_XL_API_27(AVD) - 8.1.0] FAILED
androidx.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,权限在应用程序信息设置中显示为已授予,但在 api 版本 27(或更低版本)的模拟器上运行测试时仍然要求权限
有人可以确认这是否是某些 android 插件中的错误,或者我在这里遗漏了什么。
您正在使用ContextCompat'checkSelfPermission来检查应用程序是否有权限。这与支持库向后兼容,但与 androidx 不确定。替代方案可以是使用 的PermissionCheckerapi checkSelfPermission,
对于 API 级别 22 及以下,
int permission = PermissionChecker.checkSelfPermission(context, permission);
if (permission == PermissionChecker.PERMISSION_GRANTED) {
// permission is granted
} else {
// permission not granted
}
Run Code Online (Sandbox Code Playgroud)
但考虑到这些权限RECORD_AUDIO是WRITE_EXTERNAL_STORAGE危险的权限,需要用户确认才能让我们的应用程序开始使用它。在 API 级别 23 以下,这些权限在声明时会自动授予,AndroidManifest因此解决此问题的另一种方法是仅针对 API 级别 23+ 进行验证,因为验证是有意义的。
| 归档时间: |
|
| 查看次数: |
216 次 |
| 最近记录: |