如何在Android Lollipop中为呼叫和通知启用/禁用振动?

时间:2015-08-17 13:01:16

标签: android android-5.0-lollipop android-audiomanager android-vibration android-5.1.1-lollipop

这对Lollipop无效。

启用振动:

audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
            AudioManager.VIBRATE_SETTING_ON);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
            AudioManager.VIBRATE_SETTING_ON);

禁用振动:

audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
            AudioManager.VIBRATE_SETTING_OFF);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
            AudioManager.VIBRATE_SETTING_OFF);

这将在Kitkat中发挥出色。

1 个答案:

答案 0 :(得分:0)

You can control the vibration on Lollipop with this code:

Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", vibrate?1:0);

Also you need to add this permission to your manifest file:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Please note however, that the above code won't allow you to change the vibration while the phone rings. I guess the phone app reads this setting before the ringing starts, and uses it until the ringing stops.