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);
}
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);
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);
}
I have the following code in a BroadcastReciver and for some reason won't set the volume to 0.
If I put the same code that sets the volume attached to a button on click event it works just fine. Can someone shed some light as to why this might possibly happen? I know the broadcast receiver is being invoked because I put a log message inside the if statement and it displayed.
public void onReceive(Context context, Intent intent) {
int volume = CommonUtilities.getSharedPreferenceInt(context, CommonUtilities.PREF_FILE, CommonUtilities.PERCENT, -1);
boolean savedSettings = CommonUtilities.getSharedPreferenceBoolean(context, CommonUtilities.PREF_FILE, CommonUtilities.SAVED_SETTINGS, false);
if(volume >= 0 && savedSettings){
Log.v(CommonUtilities.TAG, "Setting the audio");
CommonUtilities.setSharedPreferenceBoolean(context, CommonUtilities.PREF_FILE, "SetAudio", true);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
}
}
If I put this same code in the onClick event it works just fine. The volume is set to 0 and shows the volume UI that it's set to 0.
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
Thank you for helping!
i made a BroadcastReceiver that is listening for changes in the PHONE_STATE. in the onReceive method, i'd like to turn off the system vibrator. i tried different approaches, but non of them worked so far.
AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
systemVibration = audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
or
Vibrator vib = (Vibrator)ctx.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();
or
System.putInt(ctx.getContentResolver(), System.VIBRATE_ON, 0);
or all of them together.
the first approach with the AudioManager really changes the system setting for the vibration, but it does not affect the currently ongoing one.
any ideas?
Simon
Stopping Vibration started by other process is not allowed in android now and thus this hack could stop Vibration or it will give you a feel that it has stopped vibration.
long timea = System.currentTimeMillis();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
while ((System.currentTimeMillis() - timea) < 15000) {
v.vibrate(1);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
Try this: (borrowed and modified from Android Source)
AudioManager am = Context.getSystemService(Context.AUDIO_SERVICE);
boolean vibeInSilent = false;
int callsVibrateSetting = AudioManager.VIBRATE_SETTING_OFF;
Settings.System.putInt(getContentResolver(),
Settings.System.VIBRATE_IN_SILENT,
vibeInSilent ? 1 : 0);
//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//or (not sure which one will work)
//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
callsVibrateSetting);
I need to get the right settings for vibrator so that my application respects the device's sound settings. On the following code, if the vibrator is off(on my phone when I lower the volume, it is a state when the volume is off and vibrator is off and one when volume is off an vibrator is on). When the phone is set to no vibrate (verified by making a call to this device), I still get the vibrator as being on:
AudioManager audioManager = (AudioManager) PingerApplication.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
int vibrationSetting = audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION);
boolean vibrate;
switch(vibrationSetting) {
case AudioManager.VIBRATE_SETTING_ON:
vibrate = true;
break;
case AudioManager.VIBRATE_SETTING_OFF:
vibrate = false;
break;
case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
vibrate = (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
break;
default:
vibrate = false;
}
Am I doing something wrong? vibrationSetting is always AudioManager.VIBRATE_SETTING_ON
You can also put check on AudioManager.getRingerMode(), e.g.
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
{
// should not vibrate
}
According to the Javadoc, you should use AudioManager.shouldVibrate(int) instead.