Reputation:
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
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
Reputation:
Thanks goes to @Luksprog who answered in the comments.
Vivrator vib = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
Upvotes: 1