Android;检测设备中的“振动”设置是打开还是关闭,尤其是在振铃但不振动的情况下 [英] Android; detect if the Vibrate setting in the device is on or off, esp for the case when it rings but does not vibrate

查看:363
本文介绍了Android;检测设备中的“振动”设置是打开还是关闭,尤其是在振铃但不振动的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



相关信息:



首先,



RINGER_MODE_VIBRATE 涵盖的场景:



检测到的是问题:



从Android M,Nexus 6获得的屏幕截图

解决方案

更新:



解决方案有点在其中一个循环中使用不推荐使用的方法扭绞,它已在我测试过的所有设备上工作;公司的;摩托罗拉,三星,htc,lg,小米,micromax,索尼

  audioManager =(AudioManager)getApplicationContext()。getSystemService(Context。 AUDIO_SERVICE); 

if(audioManager.getRingerMode()!= AudioManager.RINGER_MODE_SILENT){
//如果(audioManager.getRingerMode()== AudioManager.RINGER_MODE_VIBRATE确保它不在静音
){
//如果处于振动模式,尤其是api 23
}} if(audioManager.getStreamVolume(AudioManager.STREAM_RING)!= 0){
// else检查是否音量不是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){//需要添加它以检测小米设备
中的特定情况//如果设备的操作系统版本不是23,则此部分有效
}
}
if((1 == Settings.System.getInt(ApplicationController.getInstance()。getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING,0)))){
//在API 23中添加了常量VIBRATE_WHEN_RINGING
}
}


App requirement: To detect when a phone is on Ringing mode with the Vibrate Setting being OFF so the problem comes down to : detect whether the Vibrate Setting is OFF or ON

Relevant information:

To start with, Vibrator has no method like isVibratorModeOn() and the Flags related to Vibrate and EXTRA_VIBRATE_... are all marked deprecated with:

This constant was deprecated in API level 16. Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().

But under getRingerMode(), we won't exactly know if the vibrate setting is off or not with RINGER_MODE_NORMAL;

It will vibrate if the vibrate setting is on.

Now the getVibrateSetting() is also deprecated.

Code which works for detecting the RINGER_MODE on *most devices and o.s. versions including Android M on Nexus 6:

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

*Exception: Nexus 5 - 5.1.1, probably for all Lollipop versions

I feel its very surprising if this has been missed or i've made the big(gest) silly mistake by missing something obvious. Hopefully, this isn't a waste of your time.

Here's more to explain the problem:
RINGER_MODE_SILENTscenario covered:

RINGER_MODE_VIBRATE scenario covered:

DETECTING THIS IS THE PROBLEM:

Screenshot crops from Android M, Nexus 6

解决方案

Update:

the solution is a bit twisted, using a deprecated method in one of the loops, it has worked on all devices i have tested; those of companies; motorola, samsung, htc, lg, xiaomi, micromax, sony

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
       }
}

这篇关于Android;检测设备中的“振动”设置是打开还是关闭,尤其是在振铃但不振动的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆