When I open my second activity, I would like it to turn on Do Not Disturb mode on my android device. However, I want Do Not Disturb to only turn on through the second page (either by opening it or through a button created on the second page). The only code I found on stack overflow was in my Android Manifest File
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
I'm not sure where to go from here, any help is appreciated.
Use that method:
private void setRingerMode(Context context, int mode) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Check for DND permissions for API 24+
if (android.os.Build.VERSION.SDK_INT < 24 || (android.os.Build.VERSION.SDK_INT >= 24 && !nm.isNotificationPolicyAccessGranted())) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(mode);
}
}
Where mode parameter can be AudioManager.RINGER_MODE_SILENT or
AudioManager.RINGER_MODE_NORMAL
Related
audiomanager.setRingerMode RINGER_MODE_SILENT has no effect. There is no error.
RINGER_MODE_VIBRATE and RINGER_MODE_NORMAL work fine. In the problematic code, switching from RINGER_MODE_SILENT to RINGER_MODE_VIBRATE produces a switch to vibrate. A previous question suggest resolving this problem by switching to do not disturb mode. https://code-examples.net/en/q/255675d . I implemented this code with no effect.
The relevant android manifest code is:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"
/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
The onStartCommand method of the service is:
//#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Context context = getApplicationContext();
CharSequence text = "sound mode set silent from sss service!";
int duration = Toast.LENGTH_LONG;
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.setInterruptionFilter
(NotificationManager.INTERRUPTION_FILTER_ALARMS);
}
try {
AudioManager myAudioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
// myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
} catch (Exception e) {
e.printStackTrace();
}
stopSelf();
return mStartMode;
}
The only Android device I have available to test this is Android 9, Pie.
How do I set the audio profile in settings to silent programmatically, please?
Try this, I've seen this work for some users:
myAudioManager.setRingerMode(0);
I'm also stuck on this but as far as my research has gone this is an issue with Android 9 specifically. Every device I've tested this snippet on has worked but for some reason android 9 just enables do not disturb mode when below code runs.
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Hey there I am using below code to turn the device into silence mode.
public static void setDeviceOnSilent(Context context) {
AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
The above code work well in all device except Gionee A1. Please suggest what is the problem i already given the NotificationAccessPolicyEnabled Permission. below is the code.
public static boolean isNotificationAccessPolicyEnabled(Context context) {
NotificationManager notificationManager;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
context.startActivity(intent);
return false;
}
return true;
}
The code looks pretty much alright. Check the manifest for the necessary permissions.
I am a beginner Android developer and I have an interesting question at hand. I am trying to mute the phone with
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
But I am getting the following error
java.lang.SecurityException: Not allowed to change Do Not Disturb state
Now I do not understand this, as I am using EasyPermissions (https://github.com/googlesamples/easypermissions) and requesting the permission
Manifest.permission.ACCESS_NOTIFICATION_POLICY
But it does not ask me to allow anything on app startup. I figured this is because ACCESS_NOTIFICATION_POLICY is a non dangerous permission and thus granted at installation time, for which I also added it to my manifest.xml as thus
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
But it is still not working, my app is crashing because it throws the "Not allowed to change to Do Not Disturb state" error. Wierdly I found that I can request to go to the "Do Not Disturb access" screen with the following code
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
What am I doing wrong here? Why cannot I request this permissions as a normal permission? Do I really have to go through the intent to allow my app to mute the phone?
From the AudioManager#setRingerMode() reference:
From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed unless the app has been granted Do Not Disturb Access.
From API level 23 and onward, you have to declare ACCESS_NOTIFICATION_POLICY permission in the manifest AND then the user needs to grant your app access to toggle Do Not Disturb. You can check if the access is granted with NotificationManager#isNotificationPolicyAccessGranted(). If your package do not have access, start an ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS intent so the user can give your app access.
NotificationManager n = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if(n.isNotificationPolicyAccessGranted()) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}else{
// Ask the user to grant access
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent);
}
I will post an answer on how I managed to solve this issue for androids over 23 and under 23 with a startActivityForResult callback
the requestMutePhonePermsAndMutePhone() function is called in the main actions onCreate function.
Mind you the code is very much intrusive, since the Settings menu is permacalled until you accept the Do Not Disturb permissions, but one can easily accomodate this code for their personal needs and maybe build a question prompt, etc...
private void requestMutePhonePermsAndMutePhone() {
try {
if (Build.VERSION.SDK_INT < 23) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else if( Build.VERSION.SDK_INT >= 23 ) {
this.requestDoNotDisturbPermissionOrSetDoNotDisturbApi23AndUp();
}
} catch ( SecurityException e ) {
}
}
private void requestDoNotDisturbPermissionOrSetDoNotDisturbApi23AndUp() {
//TO SUPPRESS API ERROR MESSAGES IN THIS FUNCTION, since Ive no time to figrure our Android SDK suppress stuff
if( Build.VERSION.SDK_INT < 23 ) {
return;
}
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if ( notificationManager.isNotificationPolicyAccessGranted()) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else{
// Ask the user to grant access
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult( intent, MainActivity.ON_DO_NOT_DISTURB_CALLBACK_CODE );
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == MainActivity.ON_DO_NOT_DISTURB_CALLBACK_CODE ) {
this.requestDoNotDisturbPermissionOrSetDoNotDisturbApi23AndUp();
}
}
I'm currently trying to set a phone running android 5.1 to the priority mode.
I tried to set it to silent mode in the AudioManager but this shows no effect as well as setting it to zero.
Setting it to Vibration-Mode works though...
//Neither this
AudioManager am = (AudioManager) getBaseContext().getSystemService(AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//nor this works
AudioManager am = (AudioManager) getBaseContext().getSystemService(AUDIO_SERVICE);
am.setRingerMode(0);
I haven't found any other solution by now.
Also I can't use any root features.
EDIT: Just found out that setting it to 0 (or RINGER_MODE_SILENT) does not do nothing: It takes me out of Vibration mode if I'm in...
Just found out that I can achieve it through the NotificationListener Service.
(And that my question already has an answer somewhere else...)
//In the Service I use this to enable and disable silent mode(or priority...)
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean start = intent.getBooleanExtra("start", false);
if(start)
{
Log.d("TAG","START");
//Check if at least Lollipop, otherwise use old method
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
requestInterruptionFilter(INTERRUPTION_FILTER_NONE);
else{
AudioManager am = (AudioManager) getBaseContext().getSystemService(AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
else
{
Log.d("TAG","STOP");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
requestInterruptionFilter(INTERRUPTION_FILTER_ALL);
else{
AudioManager am = (AudioManager) getBaseContext().getSystemService(AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
return super.onStartCommand(intent, flags, startId);
}
I need to enable and disable the vibration mode of mobile when user turns off and turns on the switch button .
I have tried the code below, but it's not working:
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Toast.makeText(this, "in setting "+(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE),1).show();
if(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE) {
//myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
}
else
{
//myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
}
We can enable and disable the silent mode programmatically by using AudioManager:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for setting silent mode :
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
For normal mode :
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
First of all use this permission in AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>
Now
public void startVibrate(View v) {
long pattern[] = { 0, 100, 200, 300, 400 };
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, 0);
}
public void stopVibrate(View v) {
vibrator.cancel();
}
Vibrate pattern
public abstract void vibrate (long[] pattern, int repeat)
Pattern for vibration is nothing but an array of duration's to turn ON and OFF the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator ON. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values, alternates between ON and OFF.
long pattern[]={0,100,200,300,400};
If you feel not to have repeats, just pass -1 for 'repeat'. To repeat patterns, just pass the index from where u wanted to start. I wanted to start from 0'th index and hence I am passing 0 to 'repeat'.
vibrator.vibrate(pattern, 0);
myAudioManager.setVibrateSetting();
This method was deprecated in API level 16.
you can use this one:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
RINGER_MODE_SILENT : will mute the volume and will not vibrate.
RINGER_MODE_VIBRATE: will mute the volume and vibrate.
RINGER_MODE_NORMAL: will be audible and may vibrate according to user settings.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(500);
}