如何在android中使用广播接收器振动设备?

时间:2012-06-04 07:12:56

标签: android broadcastreceiver android-vibration

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);
    }
}

在使用闹钟时使用广播接收器振动设备时,会出现如上所示的错误。这里可能出现错误的原因是什么?

2 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

你给了她许可吗?

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