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
应用程序要求:检测 phone 何时开启 振动设置关闭的振铃模式
所以问题归结为:检测振动设置是关闭还是打开
相关信息:
首先,
Vibrator 没有像 isVibratorModeOn()
这样的方法,并且
Flags related to Vibrate and EXTRA_VIBRATE_... 都标记为已弃用:
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().
但在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.
现在 getVibrateSetting() 也已弃用。
用于在*大多数设备和 o.s 上检测 RINGER_MODE
的代码。版本包括 Android M on Nexus 6:
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
试试这个,
用于检测振动
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;
}
很高兴为您提供帮助。
更新:
这个解决方案有点扭曲,在其中一个循环中使用了一种不推荐使用的方法,它在我测试过的所有设备上都有效;那些品牌;摩托罗拉、三星、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
}
}
修改后的解决方案:
即使 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;
}
应用程序要求:检测 phone 何时开启 振动设置关闭的振铃模式 所以问题归结为:检测振动设置是关闭还是打开
相关信息:
首先,
Vibrator 没有像 isVibratorModeOn()
这样的方法,并且
Flags related to Vibrate and EXTRA_VIBRATE_... 都标记为已弃用:
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().
但在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.
现在 getVibrateSetting() 也已弃用。
用于在*大多数设备和 o.s 上检测 RINGER_MODE
的代码。版本包括 Android M on Nexus 6:
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
试试这个,
用于检测振动
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;
}
很高兴为您提供帮助。
更新:
这个解决方案有点扭曲,在其中一个循环中使用了一种不推荐使用的方法,它在我测试过的所有设备上都有效;那些品牌;摩托罗拉、三星、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
}
}
修改后的解决方案:
即使 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;
}