如何在Android中以编程方式打开/关闭"请勿打扰"(dnd)?是希望这会给我一些东西,但它没有:
Settings.System.putInt(getContentResolver(), Settings.System.DO_NOT_DISTURB, 1);
Run Code Online (Sandbox Code Playgroud)
小智 36
我找到了这个解决方案
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
Run Code Online (Sandbox Code Playgroud)
这个需要:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Run Code Online (Sandbox Code Playgroud)
据我所知,在执行requestPermissions()时不会弹出请求对话框.必须通过设置菜单设置 - >声音和通知 - >请勿打扰访问来授予它.
这是在SDK 23 Marshmallow上.
Vit*_*aga 27
只是改善了Jordan Parsons的答案.要设置优先级,您需要一个权限,只有通过要求用户打开活动才能获得该权限.系统将提示是否要为您的应用程序启用"请勿打扰"权限.
此活动可以这种方式打开:
NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
授予此权限后,可以更改此设置.
小智 5
For those struggling with LG G4, there is a workaround.
LG didn't setup the settings option to grant
permission.ACCESS_NOTIFICATION_POLICY
Run Code Online (Sandbox Code Playgroud)
Therefore it crashes when it receives
ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
Run Code Online (Sandbox Code Playgroud)
However, LG did setup the option to grant
permission.BIND_NOTIFICATION_LISTENER_SERVICE
Run Code Online (Sandbox Code Playgroud)
And once this permission is granted, so is
permission.ACCESS_NOTIFICATION_POLICY
Run Code Online (Sandbox Code Playgroud)
(at least on LG G4). This option is under Settings->General->Security->Notification access. For the user to be able to grant this permission, your app needs to request for it. This is done by declaring your service, assuming you have one for your app, as follows in the manifest:
<service android:name="myService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20124 次 |
最近记录: |