启用和禁用振动机器人编程 [英] Enabling and disabling vibration in android programmatically
问题描述
在我的Android应用程序之一,我需要启用或禁用振动完全无关的模式(即沉默或一般或响亮)的。
In one of my android apps, I need to enable or disable the vibration completely irrespective of the mode (i.e. silent or general or loud).
我目前使用下面的code与德precated功能的帮助下 setVibrateSetting
I am currently using the following code with the help of the deprecated function setVibrateSetting
//对于接通振动模式
// For turning on the vibration mode
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
AudioManager.VIBRATE_SETTING_ON);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
AudioManager.VIBRATE_SETTING_ON);
//对于关闭振动模式
// For turning off the vibration mode
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
AudioManager.VIBRATE_SETTING_OFF);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
AudioManager.VIBRATE_SETTING_OFF);
不过,我想知道是否有一个清晰的方式来做到这一点。
But I am wondering if there is a clear way to do this.
和意见是最欢迎的:)
问候, Jujare
Regards, Jujare
推荐答案
本德precation文本<一个href="https://developer.android.com/reference/android/media/AudioManager.html#setVibrateSetting%28int,%20int%29"相对=nofollow> setVibrateSetting()
说:
The deprecation text for setVibrateSetting()
says:
此方法仅用于那些更换应用 音频设置或主电话平台级管理 应用程序。
This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.
据我了解,有全局启用或禁用的振动,所以如果你有一个应用程序,如环文件管理器,你可能需要使用它没有其他选择。
From what I understand, there is no other option to globally enable or disable vibration, so if you have an app such as a ring profile manager, you probably need to use it.
恕我直言,谷歌使用德precation这里不合适。
IMHO, Google used deprecation here inappropriately.
我用下面的类躲去precation一体COMPAT类:
I use the following class to hide the deprecation in one "compat" class:
@SuppressWarnings("deprecation")
class AudioManagerCompat {
final static int VIBRATE_TYPE_RINGER = AudioManager.VIBRATE_TYPE_RINGER;
final static int VIBRATE_TYPE_NOTIFICATION = AudioManager.VIBRATE_TYPE_NOTIFICATION;
final static int VIBRATE_SETTING_ON = AudioManager.VIBRATE_SETTING_ON;
final static int VIBRATE_SETTING_OFF = AudioManager.VIBRATE_SETTING_OFF;
final static int VIBRATE_SETTING_ONLY_SILENT = AudioManager.VIBRATE_SETTING_ONLY_SILENT;
static int getVibrateSetting(AudioManager am, int vibrateType) {
return am.getVibrateSetting(vibrateType);
}
static void setVibrateSetting(AudioManager am, int vibrateType, int vibrateSetting) {
am.setVibrateSetting(vibrateType, vibrateSetting);
}
}
这篇关于启用和禁用振动机器人编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!