【发布时间】:2013-01-28 09:07:44
【问题描述】:
由于AudioManager.RINGER_MODE_VIBRATE 也会使设备静音,您如何打开振动并保持当前音量(例如,如果有来电,手机会振动并播放铃声)?
谢谢!
【问题讨论】:
标签: android audio vibration android-audiomanager
由于AudioManager.RINGER_MODE_VIBRATE 也会使设备静音,您如何打开振动并保持当前音量(例如,如果有来电,手机会振动并播放铃声)?
谢谢!
【问题讨论】:
标签: android audio vibration android-audiomanager
我能想到的最简单的方法是使用:
setVibrateSetting(AudioManagerVIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
虽然该方法在 API 16 中已被弃用,但如果您希望更改振动设置而不是您自己的应用程序,我不知道有什么好的替代方法。
只要确保铃声模式也开启AudioManager.RINGER_MODE_NORMAL。
当然,您也可以将振铃模式设置为AudioManager.RINGER_MODE_NORMAL,然后调出设置页面供用户手动更改振动设置。
【讨论】:
查看此代码,
// Get instance of Vibrator from current Context
Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start immediately
// Vibrate for 200 milliseconds
// Sleep for 500 milliseconds
long[] pattern = { 0, 200, -1 };
// The "0" means to repeat the pattern starting at the beginning
// CUIDADO: If you start at the wrong index (e.g., 1) then your
// pattern
// will be off --
// You will vibrate for your pause times and pause for your vibrate
// times !
vb.vibrate(pattern, 0);
停下来,打电话
vb.cancel();
注意: 不应在 vb.vibrate(pattern, 0); 之后立即调用 vb.cancel();
【讨论】: