0

我制作了一个正在监听 PHONE_STATE 变化的 BroadcastReceiver。在 onReceive 方法中,我想关闭系统振动器。我尝试了不同的方法,但到目前为止都没有奏效。

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

或者

Vibrator vib = (Vibrator)ctx.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();

或者

System.putInt(ctx.getContentResolver(), System.VIBRATE_ON, 0);

或全部一起。

AudioManager 的第一种方法确实改变了振动的系统设置,但它不影响当前正在进行的设置。

有任何想法吗?

西蒙

4

2 回答 2

1

停止由其他进程启动的振动现在在 android 中是不允许的,因此这个 hack 可以停止振动,或者它会给你一种它已经停止振动的感觉。

长时间a = System.currentTimeMillis(); 振动器 v = (振动器) getSystemService(Context.VIBRATOR_SERVICE);

而 ((System.currentTimeMillis() - timea) < 15000) { v.vibrate(1); 尝试 { Thread.sleep(10); } 捕捉 (InterruptedException e) {} }

于 2014-01-02T12:11:02.897 回答
0

试试这个:(从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);
于 2011-08-24T21:29:00.883 回答