我写了一个Android应用程序。现在,我想让设备在某个动作发生时震动。我该怎么做呢?


当前回答

上面的答案是非常正确的,但我给出了一个简单的步骤:

 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>

这是最简单的方法。

其他回答

未经允许震动

如果你想简单地震动设备一次,以提供对用户操作的反馈。你可以使用视图的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>标签外添加。

已弃用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);
    }
}

我努力理解如何做到这一点在我的第一个实现-确保你有以下:

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);    
 }

用这个:

import android.os.Vibrator;
     ...
     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
     // Vibrate for 1000 milliseconds
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(1000,VibrationEffect.DEFAULT_AMPLITUDE));
     }else{
     //deprecated in API 26 
            v.vibrate(1000);
     }

注意:

不要忘记在AndroidManifest.xml文件中包含权限:

<uses-permission android:name="android.permission.VIBRATE"/>