简体   繁体   中英

Vibration on Android 9

I have this vibration method that makes my app vibrate exactly how I want on my phone (Android 6 API23).

However when my friend tried it on his phone (Android 9) it did not work. It vibrated in the wrong way, constantly, and he could not even turn it off with the button in the app.

How can I make my vibration work exactly the same on newer versions of Android?

long[] pattern = { 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};

This is the code for the vibration

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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.d("VIBRATE ===>", "I AM VIBRATING");

               // v.vibrate(VibrationEffect.createWaveform(pattern, 0)); This did not work

            } else {
                Log.d("VIBRATE ====>", "I AM VIBRATING");


                v.vibrate(pattern , 0); //This do work on Android 6
            }

This is the method that stops the vibration. (Again works on Android 6)

public static void stopVibration() {
        v.cancel();

}

Edit my attempt

So I want this to vibrate for 1 second, pause for 1 second, vibrate for 1 second and so on until a button is pressed. Do you think this code would work for Android 9 (or API > 26 I guess)?

 long[] mVibratePattern = new long[]{ 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
 int[] mAmplitudes = new int[]{0, 255, 255, 255, 255, 255, 255, 255};



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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.d("VIBRATE ===>", "I AM VIBRATING");


                    VibrationEffect effect = VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0); //Will this work on Android 9?
                    v.vibrate(effect);


            } else {
                Log.d("VIBRATE ====>", "I AM VIBRATING");


                v.vibrate(pattern , 0); //This do work on Android 6
            }

So i got my hands on a Android 9 phone and this made the vibration work the same on Android 9.

long[] pattern = { 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
int[] mAmplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0};

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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.d("VIBRATE ===>", "I AM VIBRATING");


                    VibrationEffect effect = VibrationEffect.createWaveform(pattern, mAmplitudes, 0); //This do work on Android 9
                    v.vibrate(effect);




            } else {
                Log.d("VIBRATE ====>", "I AM VIBRATING");


                v.vibrate(pattern , 0); //This do work on Android 6
            }

I have this vibration method that makes my app vibrate exactly how I want on my phone (Android 6 API23).

However when my friend tried it on his phone (Android 9) it did not work. It vibrated in the wrong way, constantly, and he could not even turn it off with the button in the app.

How can I make my vibration work exactly the same on newer versions of Android?

long[] pattern = { 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};

This is the code for the vibration

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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.d("VIBRATE ===>", "I AM VIBRATING");

               // v.vibrate(VibrationEffect.createWaveform(pattern, 0)); This did not work

            } else {
                Log.d("VIBRATE ====>", "I AM VIBRATING");


                v.vibrate(pattern , 0); //This do work on Android 6
            }

This is the method that stops the vibration. (Again works on Android 6)

public static void stopVibration() {
        v.cancel();

}

Edit my attempt

So I want this to vibrate for 1 second, pause for 1 second, vibrate for 1 second and so on until a button is pressed. Do you think this code would work for Android 9 (or API > 26 I guess)?

 long[] mVibratePattern = new long[]{ 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
 int[] mAmplitudes = new int[]{0, 255, 255, 255, 255, 255, 255, 255};



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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.d("VIBRATE ===>", "I AM VIBRATING");


                    VibrationEffect effect = VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0); //Will this work on Android 9?
                    v.vibrate(effect);


            } else {
                Log.d("VIBRATE ====>", "I AM VIBRATING");


                v.vibrate(pattern , 0); //This do work on Android 6
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM