In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent)
Thanks for your answers, here is a little more detail.
To be able to set ringer mode to silent, you must ask permission to access notification policy (like @ucsunil said).
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Then, check if you have this permission. If you do not, open the settings for "Do Not Disturb access" for your app:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
When you run startActivity(), Android opens the Do Not Disturb access settings for your app.
What confused me, was that the way to ask for this permission is completely different from other permissions.
Just for reference, here is the way to ask for permission READ_CONTACTS:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(activity,
new String[]{ Manifest.permission.READ_CONTACTS },
REQUEST_CODE_READ_CONTACTS);
}
How to completely set ringer mode to silent?
Finally found a workaround for this problem. It seems that android still considers the phone in Silent Mode if the Do Not Disturb is on, even the ringer mode is set to Normal Mode manually. So, I need to set the ringer mode to Normal Mode first, wait it for 1 second, and then set to Silent Mode again.
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
// Wait for 1 second
// Direct changing won't set the Do Not Disturb on
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
App gets crashed after I added audio manager in android
try to add this permission in manifest.xml
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"
App not going to silent mode, possible audio manager bug?
In the recent API's we need to give access to application's for changing phone profiles.
I just needed to add this fragment.
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
Android N: checking for DND state changed before updating checkbox preference
I managed to solve this and will post the answer in case someone else needs it.
A BroadcastListener is not needed in this case. You just need to request the permission when the user first clicks on the preference with an OnPreferenceClickListener
:
private void requestNotificationPolicyAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.
ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
If the user grants the permission, when he will click again, you will allow the user to change the state of the CheckBox
. If the permission is not granted, you will continue updating the CheckBox
with "false" and maybe write in it's android:summary
that permission must be granted first. You can verify with:
private boolean isNotificationPolicyAccessGranted() {
NotificationManager notificationManager = (NotificationManager)
getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
return notificationManager.isNotificationPolicyAccessGranted();
}
Is it possible to turn off the silent mode programmatically in android?
Solution for you .
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
//For Normal mode
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
//For Silent mode
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//For Vibrate mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Related Topics
How to Pass a View's Onclick Event to Its Parent on Android
Lazy Download Images into Gridview
Only Load Layout When Firebase Calls Are Complete
Get List of Photo Galleries on Android
How to Change a Tab Background Color When Using Tablayout
Get Ssid When Wifi Is Connected
Android Maps API V2 with Custom Markers
Preventing Status Bar Expansion
What Will Happen to the Sharedpreferences on Update an Android App
Android: How to Get a Radiogroup with Togglebuttons
How to Transfer an Image from Its Url to the Sd Card
Android Facebook Sdk 3.0 Gives "Remote_App_Id Does Not Match Stored Id" While Logging In
Safely Fixing: Javax.Net.Ssl.Sslpeerunverifiedexception: No Peer Certificate
Connecting 2 Emulator Instances in Android
Forcing the Soft Keyboard Open
Android Youtube Upload Video with Static Username and Password