93

How can I request the vibrate permission in my Android application?

Hello World
  • 2,764
  • 1
  • 11
  • 23
Hardik Gajjar
  • 5,038
  • 8
  • 33
  • 51

2 Answers2

239

Here is the link to the reference

Add this to your manifest, as a direct child of the manifest element:

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

This is how you let the phone Vibrate thru code:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Vibrate for 300 milliseconds
v.vibrate(300);

For a more creative pattern try the star wars theme =D

v.vibrate(new long[]{0, 500, 110, 500, 110, 450, 110, 200, 110, 170, 40, 450, 110, 200, 110, 170, 40, 500}, -1);
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
  • 14
    hahaha, you wrote "vibrator" :'D Seriously, thanks for this answer! :) – codepleb Dec 03 '13 at 20:34
  • 3
    To be really clear... add it within your tag! Do not add it to any other tag! – Peter Arandorenko Mar 10 '14 at 20:19
  • If you can, update your answer with this: ```val vibrationEffect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE) vibrator.vibrate(vibrationEffect)```. This is for Android O and newer versions. – JJ86 Feb 07 '22 at 21:02
29

Add the following in your AndroidManifest.xml:

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

More info on AndroidManifest permissions here

More info on list of permission here

ccheneson
  • 49,072
  • 8
  • 63
  • 68