开发者

How to vibrate device n number of times through programming in android?

开发者 https://www.devze.com 2023-03-03 05:59 出处:网络
can anyone tell me how to vibrate same patter 5 times like this my pattern 开发者_如何学Python long[] pattern = { 0, 200, 500 };

can anyone tell me how to vibrate same patter 5 times like this my pattern

开发者_如何学Python

long[] pattern = { 0, 200, 500 };

i want this pattern to repeat 5 times

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);


I found the solution, it was very simple:

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , -1);


From: Android Vibrator#vibrate(long[], int)

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

You have to init index 0

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , 0);


the following works for me:

if(vibration_enabled) {
    final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if(v.hasVibrator()) {
        final long[] pattern = {0, 1000, 1000, 1000, 1000};
        new Thread(){
            @Override
            public void run() {
                for(int i = 0; i < 5; i++){ //repeat the pattern 5 times
                    v.vibrate(pattern, -1);
                    try {
                       Thread.sleep(4000); //the time, the complete pattern needs
                    } catch (InterruptedException e) {
                        e.printStackTrace();  
                    }
                }
            }
        }.start();
    }
}

The vibrate method only starts the vibration, but doesn't wait until its executed.


Your code should do the trick. Just make sure you have <uses-permission android:name="android.permission.VIBRATE"/> in the AndroidManifest.xml file.


Besides the above given solutions, i have created my own vibration pattern where i can control the duration size between vibrations. startVibration() creates a continous regular vibration pattern for one minute.

stopVibration() - Terminates the vibration or pauses the counterTimer thus pausing the vibration pattern.

private time = 0;
private countDownTimer;

private void startVibration() {
    time = (int) System.currentTimeMillis();

    countDownTimer = new CountDownTimer(60000, 1000) {

        public void onTick(long millisUntilFinished) {

            time = (int) (millisUntilFinished / 1000);
            int[] timeLapse = {58, 55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4, 1};
            for (int k = 0; k < timeLapse.length; k++) {
                if (time == timeLapse[k]) {
                    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000);
                }
            }
        }

        public void onFinish() {
        }
    }.start();
}

private void stopVibration() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
    }
}


In the method public void vibrate (long[] pattern, int repeat), the long[] pattern follows the rule : long[] pattern = {pauseTime1, vibrationTime1, pauseTime2, vibrationTime2, pauseTime3, vibrationTime3, ...} so as a result you have odd number of values, it won't works. You must have to end the pattern with a vibrationTime. An even number of value do the job (at least 4 values).

long[] pattern = {0, 500, 200, 500}
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);


Here's how I did it. I dynamically generate my timing array based on the number of times the caller wants it to vibrate. In the loop, start at 1 so to avoid 0 % 2 causing an extra useless delay at the end.

private void vibrate(int times)
{
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        ArrayList<Long> timings = new ArrayList();

        timings.add(0L);

        for(int i = 1; i <= times; i ++)
        {

            if(i%2==0)
                timings.add(0L);

            timings.add(250L);


        }

        long[] arrTimings = new long[timings.size()];

        for(int j = 0; j < timings.size(); j++)
        {
            arrTimings[j] = timings.get(j);
        }

        vibrator.vibrate(VibrationEffect.createWaveform(arrTimings, -1));
    }
}


    long vibrationDuration = Arrays.stream(pattern).sum();
    new CountDownTimer(vibrationDuration*(repeat+1), vibrationDuration) {

        @Override
        public void onTick(long millisUntilFinished) {
            v.cancel();
            if (Build.VERSION.SDK_INT >= 26) {
                VibrationEffect vibrationEffect = VibrationEffect.createWaveform(pattern, -1);
                v.vibrate(vibrationEffect);
            } else {
                v.vibrate(pattern, -1);
            }
        }

        @Override
        public void onFinish() {
            v.cancel();
        }
    }.start();


You can apply one trick, just build pattern dynamic based on number of repetition you want.

private long[] createVibrationPattern(long[] oneShotPattern, int repeat) {
    long[] repeatPattern = new long[oneShotPattern.length * repeat];
    System.arraycopy(oneShotPattern, 0, repeatPattern, 0, oneShotPattern.length);
    for (int count = 1; count < repeat; count++) {
        repeatPattern[oneShotPattern.length * count] = 500; // Delay in ms, change whatever you want for each repition
        System.arraycopy(oneShotPattern, 1, repeatPattern, oneShotPattern.length * count + 1, oneShotPattern.length - 1);
    }
    return repeatPattern;
}

Then do the call like below

long[] pattern = { 0, 200, 500 };
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(createVibrationPattern(pattern , 2);

OUTPUT Pattern would become as 0, 200, 500, 500, 0, 200, 500

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号