How to detect android phone ring and vibrate programmatically? - android

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.

Related

Android 6 AudioManager returns wrong RingerMode

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?

Which AudioFocus do I need to set for an alarm application

From this blog I learned that, for all media applications, I should request AudioFocus and start the playback only if the AudioFocus is granted.
am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);
I am making an alarm application.
As an alarm, it should always ring until dismissed - except during phone calls (it is logical).
I can track if a call is ringing or idle by using the TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
switch (telephonyManager.getCallState()) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
break;
default:
outsidePause = true;
break;
}
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, (0));
am.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
setInnerData();
startAlarm();
break;
default:
outsidePause = true;
vibrator.cancel();
pauseAlarm();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
Taking in account the TelephonyManager, is it useful and nessesary to use the AudioFocus (if it is, which mode is preferrable)?
Can another application take it from mine, making silencing the alarm?
Seems, that using Audio focus if ok. Othet application does not take it, untill I abandone it.
Hey guys you can use below code in your onAudioFocusChange() method to Manage your Audio Focus when Alarm or Music Player or WhatsApp video call or Phone call comes in all these scenario it will work
// (Below code) Mandatory method for onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Whenever Alarm is started command comes to this place
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
say = false;
mediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_GAIN :
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, pause);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(pause);
say = false;
mediaPlayer.start();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
mediaPlayer.pause();
say = false;
audioManager.abandonAudioFocus(this);
}
break;
}
}
}

How to enable and disable vibration mode programmatically

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);
}

AudioManager is unreliable

AudioManager is unreliable in onCallStateChanged. During a phone call I need it to turn on speaker phone and set the volume to max. It sometimes turns on speakerphone (usually during the second or later call) and rarely turns the volume up. My PhoneCallListener class is within my MainActivity class.
private class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
AudioManager aM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
aM.setMode(AudioManager.MODE_IN_CALL);
aM.setSpeakerphoneOn(true);
if(TelephonyManager.CALL_STATE_RINGING == state)
{
//phone ringing
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state)
{
//phone active
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
isPhoneCalling = true;
}
if(TelephonyManager.CALL_STATE_IDLE == state)
{
aM.setSpeakerphoneOn(false);
if(isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
Within CALL_STATE_OFFHOOK I had to turn off AudioManager.FLAG_SHOW_UI because it would continually show the volume UI. Also, setting aM.setStreamVolume(AudioManager.STREAM_MUSIC, aM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); crashes the app for some reason.
Any suggestions on how to make AudioManager work every time so that speakerphone is on and volume is max during a phone call?
Edit: Even with setting speackphoneon to true as soon as the onCallStateChanged method is called, it still is not reliably turning the speakerphone on. The volume is also unreliable and can't seem to set it to max without it crashing.
Below is the code to do this. I have tested in a phone running lollipop. Write your PhoneStateListener as:
private class myPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK: //Call is established
Log.d("s#urav", "Call is Offhook now!");
try {
Thread.sleep(500); //We never know when the call is actually OffHook
} catch (InterruptedException e) {
Log.d("s#urav","Exception is:"+e);
}
audioManager.setSpeakerphoneOn(true);
break;
case TelephonyManager.CALL_STATE_IDLE: //Call is finished
//Maintain a flag and do this only if speakerphone has been set on OFFHOOK
/*audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(false);*/
break;
}
}
}
For raising the volume of the call you have to increase the volume of STREAM_VOICE_CALL. This code + increasing the volume of call stream will meet you requirements.

android get vibration settings

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.

Categories

Resources