【发布时间】:2021-09-10 00:00:47
【问题描述】:
我正在使用来自 this answer 的以下代码在我的 Wear OS 应用的 onResume() 方法中检查和请求权限。
(我为检查和请求尝试了各种自行编写的代码,并且得到了相同的结果,但是这段代码似乎是一个使用起来很容易扩展的简洁小函数。)
public boolean checkAndRequestPermissions() {
Log.d("checkAndRequestPermissions", "starting check");
int internet = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE);
int loc_coarse = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int loc_fine = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (internet != PackageManager.PERMISSION_GRANTED) {
Log.d("checkAndRequestPermissions", "INTERNET missing");
listPermissionsNeeded.add(Manifest.permission.ACCESS_NETWORK_STATE);
}
if (loc_coarse != PackageManager.PERMISSION_GRANTED) {
Log.d("checkAndRequestPermissions", "COARSE_LOCATION missing");
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (loc_fine != PackageManager.PERMISSION_GRANTED) {
Log.d("checkAndRequestPermissions", "FINE_LOCATION missing");
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (!listPermissionsNeeded.isEmpty()) {
Log.d("checkAndRequestPermissions", "Requesting missing permissions");
ActivityCompat.requestPermissions((Activity) this, listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]), 1);
return false;
}
return true;
}
当我的应用程序第一次安装并运行时 - 无论是在模拟器上还是在真实设备上 - 应用程序都会启动,运行 onCreate 没有问题,进入 onResume 并调用上述方法。 (我在onCreate 和onResume 都添加了很多调试输出来查看进度,一切都很好)
我可以看到权限检查,可以看到COARSE_LOCATION 和FINE_LOCATION 都按预期丢失(ACCESS_NETWORK_STATE 似乎默认启用)。
然后我看到应用程序即将请求权限。
此时我的应用进入onPause,大概是为了显示权限请求对话框,但随后一切都停止了。
logcat 中没有更多内容,设备显示屏变黑并保持黑色。
这是 logcat 在我的应用程序的调试中显示的内容:
D/onCreate: starting
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/3words_for_wea: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, reflection)
W/3words_for_wea: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, reflection)
Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (light greylist, reflection)
D/onCreate: set up bindings and animations
D/onCreate: set up on click listeners
D/onCreate: end of onCreate
D/onResume: onResume - last w3wAddress =
D/checkAndRequestPermissions: starting check
D/checkAndRequestPermissions: COARSE_LOCATION missing
FINE_LOCATION missing
Requesting missing permissions
D/OpenGLRenderer: HWUI GL Pipeline
D/onPause: onPause
D/: HostConnection::get() New Host Connection established 0xdbc22500, tid 4031
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xdb09c3a0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xdb09c3a0: ver 2 0 (tinfo 0xe7e56ab0)
D/EGL_emulation: eglMakeCurrent: 0xdb09c3a0: ver 2 0 (tinfo 0xe7e56ab0)
如果我返回设备主屏幕并重新打开应用程序,权限请求对话框会立即打开,我可以授予权限,应用程序会按预期启动。
我观察到的另一个奇怪现象是,当设备处于“黑屏但开启”状态时,我无法从 Android Studio 重新启动应用程序。 Android Studio 仍然认为应用程序正在运行(我在 IDE 工具栏中为正在运行的应用程序显示了预期的停止和重新启动按钮),但是单击 restart-with-stop 或 restart-auto 按钮什么都不做。
但是,如果我将 logcat 视图从调试切换到详细,logcat 会清空。如果我返回调试 logcat,它仍然是空的,所以就好像某些东西实际上已经崩溃了,但是我的应用程序没有它的日志。
如果我查看 system_process logcat,而不是我的应用程序,我会看到不断重复的消息,如下所示:
1874-1995/system_process I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@97cf332)
1874-1898/system_process I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@97cf332)
请注意,在确认授予所需权限之前,不会运行任何需要请求权限的操作。
什么可能导致黑屏死机,我该如何预防?
【问题讨论】:
标签: java android android-permissions wear-os android-lifecycle