如何打开振动并保持声音

时间:2013-01-28 09:07:44

标签: android audio vibrate android-audiomanager

由于AudioManager.RINGER_MODE_VIBRATE也会使设备静音,如何打开振动并保持当前的音量(例如,如果有来电,手机会振动并播放铃声)?

谢谢!

2 个答案:

答案 0 :(得分:1)

我能想到的最简单方法是使用:

setVibrateSetting(AudioManagerVIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);

虽然该方法在API 16中已弃用,但如果您希望将振动设置更改为超过自己的应用程序,我不知道该替代方法。

确保铃声模式也在AudioManager.RINGER_MODE_NORMAL上。


当然,您也可以将铃声模式设置为AudioManager.RINGER_MODE_NORMAL并调出设置页面以供用户手动更改振动设置。

答案 1 :(得分:0)

查看此代码,

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