振动在某些api级别不起作用

时间:2018-01-05 16:26:57

标签: android vibrate

我有一个用于振动功能的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);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

即使答案与开发无关,但我想回答我的问题,为什么振动不能解决通知的振动强度关闭的原因

enter image description here

参考https://android.stackexchange.com/a/158261