How to enable and disable vibration mode programmatically - android

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

Related

Set Android Volume In A Broadcast Receiver

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!

ring an alarm even when ringer is muted in android

sir, is it possible to ring an alarm even when ringer is muted? if yes, how. and also, can i set its volume to something in between and not just maxvolume? thanks in advance
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("firealert")) {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
}//end if
}//end for
Things to look at:
AudioManager.getRingerMode / setRingerMode. Set it to RINGER_MODE_NORMAL to ensure the phone isn't muted or set to vibrate. (Although you might want to check the state first, so you can store it and reset the value after your alarm is cleared.)
if ( aManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL )
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
If you want to set volume to 50% then check AudioManager.getStreamMaxVolume and then divide that by 2, for example. It would probably be better to let the user configure it, though. Use a SeekBar to have him set a preferred volume (which, again, you could reset back to normal after your alarm plays).
RingerVolumeBar = (SeekBar) findViewById(R.id.seekBar1);
RingerVolumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser)
{
aManager.setStreamVolume(AudioManager.STREAM_RING,
progress, AudioManager.FLAG_PLAY_SOUND);
// record new setting so you can load it up or reset it back later
...
}
});

setRingerMode during incoming call - RINGING

I have a task - change the ringer volume immediately when phone ringing. For example:
After detecting, that there is incoming call I need to set ringer volume to 0 (mute) and vibrator also should be disabled (if it not disabled already). Then there is delay when I need to perform another code (startComputing();). After that ringer volume should be changed to certain value (f.e.7) and vibrator should be activated. Here is my code:
public class IncomingCallReceiver extends BroadcastReceiver {
private AudioManager amanager;
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
amanager.setRingerMode(0x00000000); // no sound and no vibration
startComputing();
amanager.setRingerMode(0x00000002); // normal
amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
AudioManager.FLAG_SHOW_UI
+ AudioManager.FLAG_PLAY_SOUND);
}
}
private void startComputing() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1)The main problem is that after this: amanager.setStreamVolume(AudioManager.STREAM_RING, 7, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
ringer does not ring at all, only toast message appears that sound level changed but phone does not play any sound. How this can be changed?
2)Also there is problem that amanager.setRingerMode(0x00000000); does not change Volume and vibration immediately but just in about half a sec.
Thank You in Advance.
Jacob
I don't think there is anything you can do to overcome the 1/2 sec delay before you mute the ringer. Sometimes it works (ie very short or no delay), sometimes you will hear the ringer for a short period of time.
I think the problem you are having is with the flags passed to setStreamVolume.
Try this:
amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
AudioManager.FLAG_SHOW_UI|AudioManager.FLAG_PLAY_SOUND);
You should be using the bitwise inclusive or (|), and not simple addition (+).

Stop the system vibration on receive call in android

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

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