我有一个用于振动功能的utils类,它在某些apis中工作但在另一个工作中没有工作
api 17正在运作
api 24无效
任何人都可以指导我为什么这段代码不起作用 注意我采取了正确的许可
<uses-permission android:name="android.permission.VIBRATE" />
我的班级
public class VibrateUtils {
public static void VibrateMethod(Context mContext , String PassedValue) {
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Vibrate for 500 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 500, 1000};
switch (PassedValue){
case "1":
// Start without a delay
// Vibrate for 500 milliseconds --> First time
// Sleep for 1000 milliseconds
pattern = new long[]{0, 500, 1000};
break;
case "2":
// Start without a delay
// Vibrate for 500 milliseconds --> First time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Second time
// Sleep for 1000 milliseconds
pattern = new long[]{0, 500, 500 , 500 , 500 };
break;
case "3":
// Start without a delay
// Vibrate for 500 milliseconds --> First time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Second time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Third time
// Sleep for 1000 milliseconds
pattern = new long[]{0, 500, 1000 , 500 , 1000 , 500 , 1000};
break;
case "4":
// Start without a delay
// Vibrate for 500 milliseconds --> First time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Second time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Third time
// Sleep for 1000 milliseconds
// Vibrate for 500 milliseconds --> Fourth time
// Sleep for 1000 milliseconds
pattern = new long[]{0, 500, 1000 , 500 , 1000 , 500 , 1000 , 500 , 1000};
break;
}
// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
if(Build.VERSION.SDK_INT >= 26){
v.vibrate(VibrationEffect.createWaveform(pattern, VibrationEffect.DEFAULT_AMPLITUDE));
}else{
v.vibrate(pattern, -1);
}
}
}
答案 0 :(得分:1)