问题
I am trying create a vibration for my game(Android), basically I want a vibration to start when a collision happens, but I can't create it because the class in which my level is running is not an activity class and I don't know ho to proceed, what should I do ? Thanks.
回答1:
But your class that wants to create it should be called from an activity right? Then there is no problem to proceed like a vibration done in 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);
}
回答2:
Use this one:
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);
}
来源:https://stackoverflow.com/questions/23173322/creating-a-vibration-in-a-non-activity-class