Wear OS应用首次运行不弹出权限申请界面 仅显示黑屏如何解决
我使用了相关回答中的代码,在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不再输出任何应用相关日志,设备屏幕黑屏且无响应。
我的应用debug模式下的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重启应用,IDE仍然显示应用正在运行(工具栏展示运行中应用对应的停止、重启按钮),但点击带停止的重启或自动重启按钮均无响应。
如果我把logcat视图从debug切换到verbose,日志会被清空,切回debug视图也仍然没有日志输出,看起来像是有组件崩溃了但没有输出对应应用崩溃日志。
查看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)
注:在权限被确认授予前,应用没有运行任何需要使用申请的相关权限的逻辑。
请问导致黑屏卡死的原因是什么?应该如何避免该问题?
内容的提问来源于stack exchange,提问作者Fat Monk

