Pages

Monday, June 17, 2013

Android Vibrate on click

If you want to make the phone to vibrate when you click on a button from your application:
1. You have to declare in your AndroidManifest.xml the permission:
?
1
<uses-permission android:name="android.permission.VIBRATE"/> 



















2. Now go to the class you want to put the vibration and in onCreate put this code:

?
1
2
final Vibrator vibe = (Vibrator) yourActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
//replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);   


3. Now make a button with a click listener. Here you will call the vibe from above.

?
1
2
3
4
5
6
7
8
Button vibrateButton = new Button(this);
vibrateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {              
                 vibe.vibrate(80);//80 represents the milliseconds (the duration of the vibration)
 
            }
        });

No comments:

Post a Comment