我写了一个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);
}
}
其他回答
你可以振动设备和它的工作
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
权限是必需的,但不需要运行时权限
<uses-permission android:name="android.permission.VIBRATE"/>
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"/>
未经允许震动
如果你想简单地震动设备一次,以提供对用户操作的反馈。你可以使用视图的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())
就这么简单!
上面的答案是非常正确的,但我给出了一个简单的步骤:
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>
这是最简单的方法。
我使用下面的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"/>
你可以使用重载方法,如果你想使用不同类型的振动(模式/不定)如上所述。
推荐文章
- 警告: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++ ?