我写了一个Android应用程序。现在,我想让设备在某个动作发生时震动。我该怎么做呢?
当前回答
我努力理解如何做到这一点在我的第一个实现-确保你有以下:
1)您的设备支持振动(我的三星平板电脑不工作,所以我一直在重新检查代码-原始代码在我的CM Touchpad上完美地工作
2)你已经在AndroidManifest.xml文件中声明了应用程序级别以上的代码来允许运行。
3)在MainActivity.java中导入以下两个文件和其他导入文件: 进口android.content.Context; 进口android.os.Vibrator;
4)调用你的振动(在这个线程中已经广泛讨论了)-我在一个单独的函数中做了它,并在其他点的代码中调用它-取决于你想要使用什么来调用振动,你可能需要一个图像(Android:长时间单击按钮->执行动作)或按钮监听器,或XML中定义的可点击对象(clickable image - Android):
public void vibrate(int duration)
{
Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibs.vibrate(duration);
}
其他回答
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");
}
其次,请确保您已经允许您的应用程序振动!回到第一点。
你可以振动设备和它的工作
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
权限是必需的,但不需要运行时权限
<uses-permission android:name="android.permission.VIBRATE"/>
我努力理解如何做到这一点在我的第一个实现-确保你有以下:
1)您的设备支持振动(我的三星平板电脑不工作,所以我一直在重新检查代码-原始代码在我的CM Touchpad上完美地工作
2)你已经在AndroidManifest.xml文件中声明了应用程序级别以上的代码来允许运行。
3)在MainActivity.java中导入以下两个文件和其他导入文件: 进口android.content.Context; 进口android.os.Vibrator;
4)调用你的振动(在这个线程中已经广泛讨论了)-我在一个单独的函数中做了它,并在其他点的代码中调用它-取决于你想要使用什么来调用振动,你可能需要一个图像(Android:长时间单击按钮->执行动作)或按钮监听器,或XML中定义的可点击对象(clickable image - Android):
public void vibrate(int duration)
{
Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibs.vibrate(duration);
}
以上答案是完美的。然而,我想在点击按钮时震动我的应用程序两次,这里缺少这个小信息,因此发布给像我这样的未来读者。:)
我们必须遵循上面提到的,唯一的变化将在振动模式如下所示,
long[] pattern = {0, 100, 1000, 300};
v.vibrate(pattern, -1); //-1 is important
它会振动两次。我们已经知道
0表示延迟 100表示第一次震动100毫秒 接下来是1000ms的延迟 然后再次震动300毫秒
人们可以继续交替地提到延迟和振动(例如,0,100,1000,300,1000,300表示3次振动等等..),但请记住@Dave的话,负责任地使用它。:)
还要注意,repeat参数被设置为-1,这意味着振动将完全按照模式中提到的发生。:)
推荐文章
- 在maven中安装mvn到底做什么
- 在android中从上下文获取活动
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 如何在Java中打印二叉树图?
- 按钮背景是透明的
- String.format()在Java中格式化双重格式
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- com.jcraft.jsch.JSchException: UnknownHostKey