How to use the Broadcast Receiver in android to vibrate the device ?

2.9k Views Asked by At
public class MyBroadcastReceiver extends BroadcastReceiver{
    public void onReceive(Context context , Intent intent){
        Toast.makeText(context, "Your time is up", Toast.LENGTH_LONG).show();
        Vibrator vibrator; 
        // ERROR here (vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);
    }
}

While using the Broadcast Receiver to vibrate the device when using an Alarm, there's an error as shown above . What may be the possible reason for an error here ?

2

There are 2 best solutions below

1
On BEST ANSWER

Try this

Vibrator v;
v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
0
On

did you give permission?

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