我写了一个Android应用程序。现在,我想让设备在某个动作发生时震动。我该怎么做呢?
当前回答
<uses-permission android:name="android.permission.VIBRATE"/>
应该在<manifest>标签内和<application>标签外添加。
其他回答
Try:
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"/>
授予振动权限
在你开始实现任何振动代码之前,你必须给你的应用程序振动的权限:
<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");
}
其次,请确保您已经允许您的应用程序振动!回到第一点。
未经允许震动
如果你想简单地震动设备一次,以提供对用户操作的反馈。你可以使用视图的performHapticFeedback()函数。这并不需要在清单中声明VIBRATE许可。
在一些常见类(如Utils)中,使用以下函数作为顶级函数。项目Kt:
/**
* Vibrates the device. Used for providing feedback when the user performs an action.
*/
fun vibrate(view: View) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
}
然后在你的片段或活动的任何地方使用它,如下所示:
vibrate(requireView())
就这么简单!
<uses-permission android:name="android.permission.VIBRATE"/>
应该在<manifest>标签内和<application>标签外添加。
更新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)
}
}
推荐文章
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 使用Enum实现单例(Java)
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何设置整个应用程序在纵向模式?