How to use the Broadcast Receiver in android to vibrate the device ?
-
12-06-2021 - |
문제
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 ?
해결책
Try this
Vibrator v;
v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
다른 팁
did you give permission?
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
제휴하지 않습니다 StackOverflow