0
我想编写一个管理Android Wear 2.0设备中“请勿打扰”设置的应用程序。下面粘贴的所有代码在Android手机中都可以正常工作。ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS in android wear
读取电流中断设置需要的权限:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
在手机和可穿戴的当前状态,可以使用检索:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int state = mNotificationManager.getCurrentInterruptionFilter();
请勿打扰状态可以用这个代码中设置:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);
这需要“访问免打扰配置”。否则,安全异常:
java.lang.SecurityException: Notification policy access denied
此权限可以通过启动对手机的配置活动请求:
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
在可穿戴这会导致异常:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS }
这意味着该配置活动在可穿戴设置中不可用。是否有另一个选项可以授予应用程序这个权限?