我写了一个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>

这是最简单的方法。

其他回答

用这个:

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"/>

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"/>

你可以振动设备和它的工作

   Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
           v.vibrate(100);

权限是必需的,但不需要运行时权限

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

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"/>

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

 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>

这是最简单的方法。