开发者

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.VIBR开发者_开发知识库ATE_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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜