我写了一个Android应用程序。现在,我想让设备在某个动作发生时震动。我该怎么做呢?
当前回答
已弃用VIBRATOR_SERVICE。
所以在SDK 31及以上使用VIBRATOR_MANAGER_SERVICE
确保你在AndroidManifest.xml中添加了这个权限
<uses-permission android:name="android.permission.VIBRATE"/>
Vibrator vibrator;
VibratorManager vibratorManager;
private static final long[] VIBRATE_PATTERN = {500, 500};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT>=31) {
vibratorManager = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
vibrator = vibratorManager.getDefaultVibrator();
}
else {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createWaveform(VIBRATE_PATTERN,0));
}
else {
vibrator.vibrate(VIBRATE_PATTERN,0);
}
}
其他回答
振动模式/波:
import android.os.Vibrator;
...
// Pause for 500ms, vibrate for 500ms, then start again
private static final long[] VIBRATE_PATTERN = { 500, 500 };
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API 26 and above
mVibrator.vibrate(VibrationEffect.createWaveform(VIBRATE_PATTERN, 0));
} else {
// Below API 26
mVibrator.vibrate(VIBRATE_PATTERN, 0);
}
Plus
AndroidManifest.xml中的必要权限:
<uses-permission android:name="android.permission.VIBRATE"/>
已弃用VIBRATOR_SERVICE。
所以在SDK 31及以上使用VIBRATOR_MANAGER_SERVICE
确保你在AndroidManifest.xml中添加了这个权限
<uses-permission android:name="android.permission.VIBRATE"/>
Vibrator vibrator;
VibratorManager vibratorManager;
private static final long[] VIBRATE_PATTERN = {500, 500};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT>=31) {
vibratorManager = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
vibrator = vibratorManager.getDefaultVibrator();
}
else {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createWaveform(VIBRATE_PATTERN,0));
}
else {
vibrator.vibrate(VIBRATE_PATTERN,0);
}
}
Kotlin更新更类型安全
在项目的一些常见类(如Utils.kt)中使用它作为顶级函数
// Vibrates the device for 100 milliseconds.
fun vibrateDevice(context: Context) {
val vibrator = getSystemService(context, Vibrator::class.java)
vibrator?.let {
if (Build.VERSION.SDK_INT >= 26) {
it.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
@Suppress("DEPRECATION")
it.vibrate(100)
}
}
}
然后在代码中的任何地方调用它,如下所示:
vibrateDevice(requireContext())
解释
使用Vibrator::class.java比使用String常量更加类型安全。
我们使用let{}检查振动器的可空性,因为如果振动对设备不可用,振动器将为空。
在else子句中抑制弃用是可以的,因为警告来自较新的SDK。
我们不需要在运行时要求使用振动的许可。但是我们需要在AndroidManifest.xml中声明它,如下所示:
<uses-permission android:name="android.permission.VIBRATE"/>
授予振动权限
在你开始实现任何振动代码之前,你必须给你的应用程序振动的权限:
<uses-permission android:name="android.permission.VIBRATE"/>
确保在AndroidManifest.xml文件中包含这一行。
导入振动库
大多数ide会为你这样做,但如果你的不这样做,下面是import语句:
import android.os.Vibrator;
确保在你想要振动发生的活动中。
如何振动一个给定的时间
在大多数情况下,你会想要在短时间内,预定的时间内震动设备。您可以通过使用振动(长毫秒)方法来实现这一点。这里有一个简单的例子:
// 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的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振动(间隔)方法已弃用Android-O(API 8.0)
要支持所有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);
}
}
科特林:
// 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)
}
}
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 碎片中的onCreateOptionsMenu
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?