简体   繁体   English

Android 9 上的振动

[英]Vibration on Android 9

I have this vibration method that makes my app vibrate exactly how I want on my phone (Android 6 API23).我有这种振动方法,可以让我的应用程序在我的手机(Android 6 API23)上完全按照我想要的方式振动。

However when my friend tried it on his phone (Android 9) it did not work.但是,当我的朋友在他的手机(Android 9)上尝试时,它没有用。 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?如何使我的振动在较新版本的 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) (再次适用于 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.所以我希望它振动 1 秒,暂停 1 秒,振动 1 秒,依此类推,直到按下按钮。 Do you think this code would work for Android 9 (or API > 26 I guess)?您认为此代码适用于 Android 9(我猜是 API > 26)吗?

 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.所以我得到了一部 Android 9 手机,这使得振动在 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).我有这种振动方法,可以让我的应用程序在我的手机上按照我想要的方式振动(Android 6 API23)。

However when my friend tried it on his phone (Android 9) it did not work.但是,当我的朋友在他的手机(Android 9)上尝试时,它不起作用。 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?如何让我的振动在较新版本的 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) (再次适用于 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.所以我希望它振动 1 秒,暂停 1 秒,振动 1 秒,依此类推,直到按下按钮。 Do you think this code would work for Android 9 (or API > 26 I guess)?您认为此代码适用于 Android 9(或 API > 26 我猜)吗?

 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
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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