Android警报振动不起作用

时间:2013-08-04 09:53:48

标签: android vibrate

我创建了AlarmReceiver类,并在收到警报时添加了振动功能。在我的代码中必定存在一些错误,因为当收到警报时振动不会启动。这是代码的一部分:

        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for 500 milliseconds


    }


    private Vibrator getSystemService(String vibratorService) {
        // TODO Auto-generated method stub
        return null;

    }
}

我当然将p ermission添加到manifest。我是否需要对此代码实现getSystemService,如果我没有实现此方法,那么eclipse会向我抛出一个我需要实现此方法的错误。

1 个答案:

答案 0 :(得分:1)

您没有实现getSystemService。您需要调用它来访问Vibrator对象,然后在其上调用vibrate方法。 但是,您需要将getSystemService返回的对象强制转换为Vibrator。

Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500});