What does Notification.DEFAULT_VIBRATE do? - android

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

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?

How to detect android phone ring and vibrate programmatically?

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.

Android 4.2.1 / 4.2.2 Notifications vibrate in silent mode

Prior to Android 4.2.1 the following code worked just fine
notification.audioStreamType = AudioManager.STREAM_RING;
notification.flags = notification.flags | Notification.FLAG_INSISTENT
| Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(ID_NOTIFICATION_SOUND, notification);
Vibration is done separately, depending on the state of a preference setting.
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vibr_pattern1, 0);
Now with Android 4.2.1+ there is always vibration even if the phone is set to silent (no vibration) and set to vibrate mode, even if the app settings for vibration are set to off.
Interestingly there is no vibration when the phone is in normal mode.
As mentioned, everything worked fine before 4.2.1.
Can someone help? What did google change here? Is it related to this bug report?
Regards,Andreas
I fixed it like this:
//vibration workaround for android 4.2.1+
notification.vibrate = new long[]{0,0};
//end workaround
I manually add "no vibration" and if the user chooses vibration it's done with an Vibrator object. If not, the code above keeps everything quiet. As mentioned before, this was not necessary before 4.2.1+
I know this is old. But for some one who is looking for answer. You can try this.
AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
v.vibrate(500);
} catch (Exception e) {
e.printStackTrace();
}
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
v.vibrate(500);
break;
}

Dismiss the Keyguard without unlock sound

I am writing a replacement LockScreen, and using LayoutParams.DISSMISS_KEYGUARD as a Window flag is dismissing the Keyguard when switching on the screen and launching my LockScreen, however it always plays an lock sound when pressing the power button again, how can I surpress the lock sound?
you could use an AudioManager to turn the sound off in OnCreate then turn it back on later. You would also want to check if the sound is off to start so that you don't turn the sound on when it was off already
This to check sound state
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
case AudioManager.RINGER_MODE_VIBRATE:
silentMode = true;
break;
case AudioManager.RINGER_MODE_NORMAL:
silentMode = false;
break;
}
This to turn sound off
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
OR
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
This to turn sound on
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Of course you would probably make am a private variable so you don't need to declare it more than once. Like this.
public class MainActivity extends Activity {
//more variables
private AudioManager am;
private boolean silentMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//check sound state and set silentMode;
//more stuff
}
}
I don't really know when to turn the sound back on but a place to try maybe after the call to getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

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