我写了一个Android应用程序。现在,我想让设备在某个动作发生时振动。我该怎么做呢?
发布于 2012-12-19 18:33:24
尝试:
import android.os.Vibrator;
...
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);
}备注:
别忘了在AndroidManifest.xml文件中包含权限:
<uses-permission android:name="android.permission.VIBRATE"/>发布于 2013-06-12 21:28:11
授予振动权限
在你开始实现任何振动代码之前,你必须给你的应用程序振动的权限:
<uses-permission android:name="android.permission.VIBRATE"/>确保在AndroidManifest.xml文件中包含此行。
导入振动库
大多数IDE都可以做到这一点,但如果您的IDE不能做到这一点,下面是import语句:
import android.os.Vibrator;在你想要振动发生的活动中确保这一点。
如何在给定的时间内振动
在大多数情况下,您会想要振动设备一小段时间,预定的时间。您可以使用vibrate(long milliseconds)方法来实现这一点。下面是一个快速示例:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 400 milliseconds
v.vibrate(400);就是这样,简单!
如何无限振动
这可能是您希望设备继续无限期振动的情况。为此,我们使用vibrate(long[] pattern, int repeat)方法:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};
// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);当您准备好停止振动时,只需调用cancel()方法:
v.cancel();如何使用振动模式
如果您想要更定制的振动,您可以尝试创建自己的振动模式:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};
// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);更复杂的振动
有多个SDK可以提供更全面的触觉反馈。我用来做特效的是Immersion's Haptic Development Platform for Android。
故障排除
如果你的设备不能振动,首先要确保它可以振动:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Output yes if can vibrate, no otherwise
if (v.hasVibrator()) {
Log.v("Can Vibrate", "YES");
} else {
Log.v("Can Vibrate", "NO");
}其次,请确保你已经给了你的应用程序振动的许可!请回到第一点。
发布于 2017-08-10 13:37:43
-O(API8.0)不推荐使用Android更新2017 vibrate(interval)方法
要支持所有Android版本,请使用此方法。
// Vibrate for 150 milliseconds
private void shakeItBaby() {
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
}
}Kotlin:
// Vibrate for 150 milliseconds
private fun shakeItBaby(context: Context) {
if (Build.VERSION.SDK_INT >= 26) {
(context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
(context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(150)
}
}https://stackoverflow.com/questions/13950338
复制相似问题