在片段上使用振动器

时间:2014-04-08 08:02:59

标签: android android-fragments

我试图在片段中使用振动,但我在上下文中遇到错误。我如何在片段中使用它?谢谢,这是我的代码:

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

1 个答案:

答案 0 :(得分:2)

试试此代码

Vibrator v = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);

Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);