0

I have to enable vibration only on devices with Android 7+. In previous versions we could simply do:

AudioManager aManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

Permission android.permission.VIBRATE is set.

How can I achive this on Android 7+?

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
egmontr
  • 249
  • 3
  • 15

3 Answers3

0

I never use your code for vibration, but I guess my code below works for Android 7+ (not tested)

Try:

 import android.os.Vibrator;

 [...]

 Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);

Note: It is better to check if your device can vibrate:

if (v.hasVibrator()) {
    Log.v("Can Vibrate", "YES");
} else {
    Log.v("Can Vibrate", "NO");
}

For more explanation, take a look to a more precisely answer by Liam George Betsworth.

Hope it helps you!

Community
  • 1
  • 1
Loic P.
  • 691
  • 6
  • 18
  • I don't want to make the device to vibrate from my App. I want to enable/disable vibration in the main device settings. Like sound on, off or only vibration. – egmontr Feb 21 '17 at 13:41
0

In Android 7 you need the permission to change the device settings to toggle any setting parameter. You can see the option to grant this permission in the App Info.

You will notice a separate List Item named,

Modify system settings

Modify System Settings

and when you click on it, you will be asked to grant the permission or not.

By Default this parameter is set to No. Change this and your code will work perfectly.

Or you can add the permission,

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

to provide the access by default.

I hope this helps. This option also depends on the ROM you are on.

Q2x13
  • 534
  • 1
  • 4
  • 14
0

try this

add permission in manifest

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

then code like

   import android.os.Vibrator;
   ...........

   Vibrator viber= (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
   viber.vibrate(500); //("time in milliseconds")
Nazim ch
  • 834
  • 8
  • 20