简体   繁体   English

控制Android的震动

[英]Control vibration of Android

I use this code to set vibration: 我使用以下代码设置振动:

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

I want set vibration only when the sound of phone is enable, but so the shake is always enable. 我只想在启用手机声音的情况下设置振动,但是抖动始终是启用的。

There is a method to check the sound's state? 有没有一种方法可以检查声音的状态? So i can inhibit the vibration when the sound's state of the phone is set in silent mode. 因此,当手机的声音状态设置为静音模式时,我可以抑制振动。

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

switch (audioManager.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        //Logic
        break;
    case AudioManager.RINGER_MODE_VIBRATE:
         //Logic
        break;
    case AudioManager.RINGER_MODE_NORMAL:
         //Logic
        break;
}

Try this 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM