user2442638
user2442638

Reputation:

Implement a vibrator in a custom view

I would like to get the Vibrator service from within the view class. Is this possible?

I have tried adding this...

Vibrator vib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

... but there is an error on VIBRATOR_SERVICE - it cannot be resolved to a variable. Setting the context to getApplicationContext() doesn't help either.

Upvotes: 1

Views: 962

Answers (2)

Cristhian Escobar
Cristhian Escobar

Reputation: 131

Try this:

Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
//Vibrates for 10 seconds
vibrator.vibrate(10000);

NOTE: Make sure you add the permission tag ( <uses-permission android:name="android.permission.VIBRATE"/>) outside the application tag. This will also work with android wear.

Upvotes: 0

user2442638
user2442638

Reputation:

Thanks goes to @Luksprog who answered in the comments.

Vivrator vib = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);

Upvotes: 1

Related Questions