2

Setting. I would like to make my phone vibrate when a button is clicked. I use the code of SO question: How to make an Android device vibrate?.

The code to let the phone vibrate:

@Override
public void onClick (View view)
{
    Vibrator vibrator = (Vibrator) MyActivity.this.getSystemService (Context.VIBRATOR_SERVICE);

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

I also added the permission

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

to the manifest file.

Problem. The phone does not vibrate at all upon button click. Other apps on my phone make the phone vibrate.

Log messages.

xxxx V/Can Vibrate﹕ YES
xxxx V/Vibrator﹕ Called vibrate(long) API - PUID: 10060, PackageName: de.creamin.vibratetest
xxxx V/Vibrator﹕ vibrate - PUID: 10060, PackageName: de.creamin.vibratetest, ms: 1000, mag: -1

Phone. Samsung Galaxy S3 (GT-I9300), Android 4.3

Question. What is going on here? Do I have to configure my phone somehow to accept the vibration code?

Community
  • 1
  • 1
LaDude
  • 1,383
  • 1
  • 11
  • 27

2 Answers2

4

I found out why my phone does not vibrate. Under menu item

phone settings > My device > Sound > Vibration intensity

you can adjust the vibration intensity for Incoming call, Notification and Haptic feedback. The settings of Notification has been set to zero. Hence, my phone did not vibrate. After setting that slider to a value greater than zero, my phone vibrates on clicking the button in my app.

LaDude
  • 1,383
  • 1
  • 11
  • 27
1

No need to pass MyActivity.this may be. Try:

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE)
Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36