Requesting permissions on Android wear

2019-02-11 03:52发布

问题:

I have written an application that takes advantage of system_alert_window on android wear. Since marshmallow this permission is revoked by default. I have enabled this permission on the phone side app from the phone settings. But the permission is still denied on the android wear side app and causes the android wear side app to crash with permission denied exception. Is there a way to enable this permission on Android wear side?

回答1:

Short answer: keep your watch app on targetSdkVersion 22.

Long answer: this permission doesn't use the usual requestPermissions() flow. There's more info in the docs, but it boils down to a special Settings UI for the user to enable this permission - and it doesn't appear that Google has implemented that UI on Wear. Without it, no (non-system) app can be granted this permission, and any app which tries to use it will crash on an error like the following:

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@a597673 -- permission denied for this window type

But, this restriction only applies to apps targeting SDK level 23 (and above). So keep your app on 22 for the time being, and hopefully Google will fill this gap before you need to increment it.

UPDATE 22-Dec-2016: This issue is fixed on Android Wear 2.0. There's still no better solution than the above for Wear 1.x, but Google has implemented the permission UI for Wear 2, and when that's released watch apps should be able to link to it with the standard intent:

startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        Uri.parse("package:" + getPackageName())));


回答2:

You should dynamically request the permissions from user on wearable devices just the same way you do on the phone side. Checkout the official tutorial Requesting Permissions on Android Wear