2

应用程序要求:检测手机何时处于振铃模式且振动设置为关闭 ,因此问题归结为:检测振动设置是关闭还是打开

相关信息:

首先, Vibrator没有类似的方法,与 VibrateEXTRA_VIBRATE_...isVibratorModeOn()相关 的 标志 都标记为弃用:

此常量在 API 级别 16 中已弃用。应用程序应根据可通过 getRingerMode() 查询的当前振铃模式维护自己的振动策略。

但是在getRingerMode()下,我们无法通过RINGER_MODE_NORMAL确切地知道振动设置是否关闭;

如果振动设置打开,它将振动。

现在getVibrateSetting()也被弃用了。

用于检测RINGER_MODE大多数设备和操作系统版本的代码,包括Nexus 6 上的 Android M

 if(audioManager.getRingerMode()!=AudioManager.RINGER_MODE_SILENT){
            if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {  

*例外:Nexus 5 - 5.1.1,可能适用于所有 Lollipop 版本

如果我错过了这一点,或者我因为错过了一些明显的东西而犯了大(最大)愚蠢的错误,我感到非常惊讶。希望这不会浪费您的时间。

这里有更多解释这个问题:
RINGER_MODE_SILENT场景涵盖: 涵盖的场景 - 铃声静音模式

RINGER_MODE_VIBRATE涵盖的场景: 场景覆盖 - 振铃器振动模式

检测这是问题所在:
问题情景 - 铃声音量高,振动启动

来自 Android M、Nexus 6 的屏幕截图

4

3 回答 3

2

更新:

解决方案有点扭曲,在其中一个循环中使用不推荐使用的方法,它适用于我测试过的所有设备;品牌的那些;摩托罗拉、三星、HTC、LG、小米、Micromax、索尼

audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        
if (audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
   //ensuring it is not on silent
   if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
      //  if it is on vibrate mode , esp for api 23
   } else if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
             //  else check whether the volume is not 0
             if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
             if (audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ON  
 || audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ONLY_SILENT) { //need to add this to detect a specific scenario in xiaomi device
       // if the device o.s version is not 23 this part works
              }
            }
if ((1 == Settings.System.getInt(ApplicationController.getInstance().getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0))) { 
       // the constant VIBRATE_WHEN_RINGING was added in api 23
       }
}
于 2015-10-21T09:24:02.780 回答
2

修改后的解决方案:

即使对于 API < 23 和 API 23,这也可以正常工作。如果您不对以前的 API 使用不推荐使用的方法,这似乎很好。在三星、摩托罗拉、OnePlus、Google Pixel、Google Nexus 上测试

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                int mode = am.getRingerMode();

            switch (mode) {
                case AudioManager.RINGER_MODE_NORMAL:
                    if ((1 == Settings.System.getInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0))) {
                        // code here
                        Toast.makeText(getApplicationContext(), "=== ring + vibrate mode ===", Toast.LENGTH_LONG).show();
                    } else {
                        // code here
                        Toast.makeText(getApplicationContext(), "=== ring + no vibrate mode ===", Toast.LENGTH_LONG).show();
                    }
                    break;

                case AudioManager.RINGER_MODE_SILENT:
                    // code here
                    Toast.makeText(getApplicationContext(), "=== in silent mode ===", Toast.LENGTH_LONG).show();
                    break;

                case AudioManager.RINGER_MODE_VIBRATE:
                    // code here
                    Toast.makeText(getApplicationContext(), "=== in vibrate mode ===", Toast.LENGTH_LONG).show();
                    break;
            }
于 2017-07-21T14:51:40.410 回答
0

尝试这个,

用于检测振动

public static boolean detectVibrate(Context context){
    boolean status = false;
    AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    if(am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
        status = true;
    } else if (1 == Settings.System.getInt(context.getContentResolver(), "vibrate_when_ringing", 0)) //vibrate on
        status = true;
    return status;
}

检查振铃器是否打开。

public static boolean checkRingerIsOn(Context context){
    AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}

很高兴为您提供帮助。

于 2015-09-22T06:56:00.357 回答