[英]How to enable and disable vibration mode programmatically
I need to enable and disable the vibration mode of mobile when user turns off and turns on the switch button . 当用户关闭并打开开关按钮时,我需要启用和禁用手机的振动模式。
I have tried the code below, but it's not working: 我已经尝试了下面的代码,但它不起作用:
AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Toast.makeText(this, "in setting "+(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE),1).show();
if(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE) {
//myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
}
else
{
//myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
}
First of all use this permission in AndroidManifest.xml 首先在AndroidManifest.xml中使用此权限
<uses-permission android:name="android.permission.VIBRATE"/>
Now 现在
public void startVibrate(View v) {
long pattern[] = { 0, 100, 200, 300, 400 };
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, 0);
}
public void stopVibrate(View v) {
vibrator.cancel();
}
Vibrate pattern public abstract void vibrate (long[] pattern, int repeat) Pattern for vibration is nothing but an array of duration's to turn ON and OFF the vibrator in milliseconds. 振动模式public abstract void vibrate(long [] pattern,int repeat)振动模式只是一个持续时间的数组,可以在几毫秒内打开和关闭振动器。 The first value indicates the number of milliseconds to wait before turning the vibrator ON. 第一个值表示在打开振动器之前等待的毫秒数。 The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. 下一个值表示在关闭振动器之前保持振动器打开的毫秒数。 Subsequent values, alternates between ON and OFF. 后续值,在ON和OFF之间交替。
long pattern[]={0,100,200,300,400};
If you feel not to have repeats, just pass -1 for 'repeat'. 如果你觉得没有重复,只需传递-1代表'重复'。 To repeat patterns, just pass the index from where u wanted to start. 要重复模式,只需从您想要开始的位置传递索引。 I wanted to start from 0'th index and hence I am passing 0 to 'repeat'. 我想从第0个索引开始,因此我将0传递给'重复'。
vibrator.vibrate(pattern, 0);
We can enable and disable the silent mode programmatically by using AudioManager: 我们可以使用AudioManager以编程方式启用和禁用静默模式:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for setting silent mode : 用于设置静音模式:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
For normal mode : 对于正常模式:
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
myAudioManager.setVibrateSetting();
This method was deprecated in API level 16. 此方法在API级别16中已弃用。
you can use this one: 你可以用这个:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
RINGER_MODE_SILENT : will mute the volume and will not vibrate. RINGER_MODE_SILENT:将音量静音,不会振动。
RINGER_MODE_VIBRATE: will mute the volume and vibrate. RINGER_MODE_VIBRATE:将音量静音并振动。
RINGER_MODE_NORMAL: will be audible and may vibrate according to user settings. RINGER_MODE_NORMAL:将听到声音,并可能根据用户设置振动。
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(500);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.