我需要为振动器获得正确的设置,以便我的应用程序尊重设备的声音设置。在下面的代码中,如果振动器关闭(当我降低音量时在我的手机上,它是音量关闭且振动器关闭时的状态和音量关闭时振动器打开时的状态)。当手机设置为无振动时(通过拨打此设备进行验证),我仍然可以将振动器打开:
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;
}
我做错了吗?振动设置始终为AudioManager.VIBRATE_SETTING_ON
答案 0 :(得分:2)
您还可以检查AudioManager.getRingerMode(),例如
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
{
// should not vibrate
}
答案 1 :(得分:0)
根据Javadoc,您应该使用AudioManager.shouldVibrate(int)
代替。