我写了一个Android应用程序。现在,我想让设备在某个动作发生时震动。我该怎么做呢?
当前回答
我使用下面的utils方法:
public static final void vibratePhone(Context context, short vibrateMilliSeconds) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vibrateMilliSeconds);
}
在AndroidManifest文件中添加以下权限
<uses-permission android:name="android.permission.VIBRATE"/>
你可以使用重载方法,如果你想使用不同类型的振动(模式/不定)如上所述。
其他回答
<uses-permission android:name="android.permission.VIBRATE"/>
应该在<manifest>标签内和<application>标签外添加。
已弃用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);
}
}
上面的答案是非常正确的,但我给出了一个简单的步骤:
private static final long[] THREE_CYCLES = new long[] { 100, 1000, 1000, 1000, 1000, 1000 };
public void longVibrate(View v)
{
vibrateMulti(THREE_CYCLES);
}
private void vibrateMulti(long[] cycles) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.vibrate = cycles;
notificationManager.notify(0, notification);
}
然后在xml文件中:
<button android:layout_height="wrap_content"
android:layout_width ="wrap_content"
android:onclick ="longVibrate"
android:text ="VibrateThrice">
</button>
这是最简单的方法。
振动模式/波:
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"/>
未经允许震动
如果你想简单地震动设备一次,以提供对用户操作的反馈。你可以使用视图的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())
就这么简单!
推荐文章
- 在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