On Android 6 this issue occurred and working fine with Android 7,8,9,10, 10+.
While using the AudioManager to get the Phone ringer mode, the value return for the mute is wrong.
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("MyApp","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","Normal mode");
break;
}
For mode set to AudioManager.RINGER_MODE_SILENT, it returns the mode AudioManager.RINGER_MODE_VIBRATE, i.e the int value is 1 and not 0.
Value for other ringer profile is returned correctly.
How to get correct silent mode?
Related
I want to implement onCallStateChanged() of PhoneStateListener and use it's functionality to change audio settings when a call is received.
private final class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
Utils.log("CallStateListener");
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Utils.log("Idle");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Utils.log("OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(prefs.getBoolean(Constants.PREF_OPTION_1, false)){
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxStreamValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
int volumeLevel = prefs.getInt(Constants.PREF_VOLUME_LEVEL, Constants.PREF_VOLUME_LEVEL_DEFAULT);
int volume = (int) Math.ceil(maxStreamValue * (volumeLevel / 100.0));
if(prefs.getBoolean(Constants.PREF_OPTION_3, false)){
Utils.log("Option3 true ringing");
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
Utils.changeNotificationVolume(audioManager, volume);
Utils.log("RINGING");
}
break;
}
callState = state;
}
}
I want to silent just the notifications if Option 1 and Option 3 are on, but a lots of new companies provide only one option for notifications and calls.
If Option 1 and Option 3 are on, i have set my device to SILENT MODE, but whenever a call is received i want my device to ring. Therefore in CALL_STATE_RINGING, i have my code to turn off SILENT MODE and ring the device.
This things only works sometime. After trying a lots of devices i have found out that usually this thing doesn't work on mobiles which lags a lot( are really slow).
I think this is because if audio settings are changed before mobile starts ringing this work, and if not this doesn't work even if settings are changed. Am i right? Is there any solution to this problem? Is there a way to delay ringing of android device? Or can i do something to make it work every time?
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("MyApp","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","Normal mode");
break;
}
From above code only detect only one mode. but i want to check 2 mode either ring+vibrate or silent+vibrate.
How it is possible?
There is no any method to get ring+vibrate and silent+vibrate. As we know that we have three method to get ringer mode.
AudioManager.RINGER_MODE_NORMAL
AudioManager.RINGER_MODE_SILENT
AudioManager.RINGER_MODE_VIBRATE
So , You just have to create a method to check condition for both ring and vibrate
Like ring+vibrate.
public boolean statusRingVibrate(){
boolean status = false;
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL && am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
status = true;
}
return status;
}
I had the same problem, and combine some method together:
public static boolean checkVibreationIsOn(Context context){
boolean status = false;
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
status = true;
} else if (1 == Settings.System.getInt(context.getContentResolver(), "vibrate_when_ringing", 0)) //vibrate on
status = true;
return status;
}
public static boolean checkRingerIsOn(Context context){
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
return am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}
Hey Please follow the link
RINGER_MODE_NORMAL : Ringer mode that may be audible and may vibrate. It will be audible if the volume before changing out of this mode was audible. It will vibrate if the vibrate setting is on.
RINGER_MODE_VIBRATE : Ringer mode that will be silent and will vibrate. (This will cause the phone ringer to always vibrate, but the notification vibrate to only vibrate if set.)
So AudioManager.RINGER_MODE_NORMAL i.e., '2' will be returned if the phone is either vibrating or ringing. And it will return AudioManager.RINGER_MODE_VIBRATE i.e., '1' if the phone is in silent and vibrating.
How does Notification.DEFAULT_VIBRATE work? If I set:
notification.defaults |= Notification.DEFAULT_VIBRATE;
what's going to happen?
The documentation is not clear. How can I make the phone vibrating if and only if the vibrate-option for the native sms application or for the call is set to true?
You need to add following permition for enabling vibrate.
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
And for detecting vibrate mode you can use AudioManager's getRingerMode() method
AudioManager audiomanager = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
switch (audiomanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("Mode","Silent mode");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("Mode","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("Mode","Normal mode");
break;
}
EDIT
You can check user's vibrate settings of call and notification using following code
Log.i("Setting", ""+audiomanager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER));
Log.i("Setting", ""+audiomanager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION));
I want to create an android application that changes the mode to ringing from vibration when incoming call from particular number
For Changing ringing from vibration on incoming call use Use TelephonyManager,AudioManager and PhoneStateListener as:
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
//CHECK YOUR PARTICULAR NUMBER HERE
if(incomingNumber=="1234567890")
{
// USE AudioManager for Settingringing from vibration
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","NORMAL mode");
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
}
}
else
{
//DO SOMETHING HERE
}
break;
default:
break;
}
}
}
add <uses-permission android:name="android.permission.READ_PHONE_STATE"> permission in manifest.xml
or How we Get Phone State using BroadcastReceiver see this tutorial:
Get Phone State When Someone is calling using BroadcastReceiver Example
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.