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

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

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

I want this pattern to be repeated 5 times

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
+7
source share
8 answers

I found a solution, it was very simple:

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

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 vibrational method starts vibration only, but does not wait for its completion.

+4
source

. , <uses-permission android:name="android.permission.VIBRATE"/> AndroidManifest.xml.

+3

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

, , , -1, .

0

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

, , . startVibration() .

stopVibration() - counterTimer , .

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();
    }
}
+2

public void vibrate (long [] pattern, int repeat) [] : long [] pattern = {pauseTime1, vibrTime1, pauseTime2, vibrTime2, pauseTime3, vibrTime3,...}, , . waveTime. ( 4 ).

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

. , , . 1, 0% 2, .

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));
    }
}
0
    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();
0

All Articles