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


当前回答

更新2017振动(间隔)方法已弃用Android-O(API 8.0)

要支持所有Android版本,请使用此方法。

// Vibrate for 150 milliseconds
private void shakeItBaby() {
    if (Build.VERSION.SDK_INT >= 26) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
    } else {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
    }
}

科特林:

// Vibrate for 150 milliseconds
private fun shakeItBaby(context: Context) {
    if (Build.VERSION.SDK_INT >= 26) {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE))
    } else {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(150)
    }
}

其他回答

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

更新2017振动(间隔)方法已弃用Android-O(API 8.0)

要支持所有Android版本,请使用此方法。

// Vibrate for 150 milliseconds
private void shakeItBaby() {
    if (Build.VERSION.SDK_INT >= 26) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
    } else {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
    }
}

科特林:

// Vibrate for 150 milliseconds
private fun shakeItBaby(context: Context) {
    if (Build.VERSION.SDK_INT >= 26) {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE))
    } else {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(150)
    }
}

未经允许震动

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

就这么简单!

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

用这个:

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