在非活动类中创建振动?

时间:2014-04-19 17:39:35

标签: android vibration

我正在尝试为我的游戏(Android)创建振动,基本上我想在碰撞发生时启动振动,但我无法创建它,因为我的关卡运行的类不是活动类,我不知道要继续,我该怎么办?感谢。

2 个答案:

答案 0 :(得分:2)

但是你想要创建它的类应该从一个活动中调用吗?然后就像在Activity Class中完成振动一样没有问题:

public function vibrate(Context context){
    // Get instance of Vibrator from current Context
    Vibrator v = (Vibrator) getSystemService(context);

    // Vibrate for 300 milliseconds
    v.vibrate(300);
}

答案 1 :(得分:1)

使用这个:

public void startVibrate(Context context, int repeat) {
    vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    int dot = 200;          // Length of a Morse Code "dot" in milliseconds
    int dash = 500;         // Length of a Morse Code "dash" in milliseconds
    int short_gap = 200;    // Length of Gap Between dots/dashes
    int medium_gap = 500;   // Length of Gap Between Letters
    int long_gap = 1000;    // Length of Gap Between Words
    long[] pattern = {
            0,  // Start immediately
            dot, short_gap, dot, short_gap, dot, medium_gap,    // S
            dash, short_gap, dash, short_gap, dash, medium_gap, // O
            dot, short_gap, dot, short_gap, dot, long_gap       // S
    };
    vibrator.vibrate(pattern, repeat);
    //vibrator.vibrate(10000);
}