How to make an Android app to vibrate phone indefinitely?

1k Views Asked by At

I want to make an Android app to make a phone vibrate indefinitely. I tried the approach recommended in different other threads but it does not seem to work. The phone vibrates, but after a moment it stops for a small fraction of second before restarting. And the pattern repeats itself every ~1 second. It does not matter the value I put in patternT, the behavior is always the same. Is it possible to make the phone vibrate indefinitely (without any lag or stop) ? The phone used is a Google Pixel 4 on Android 11.

Here is my code :

public class MainActivity extends AppCompatActivity {

    private void vibrate() {
        long[] patternT = new long[]{0, 50};
        int[] patternA = new int[]{0, VibrationEffect.DEFAULT_AMPLITUDE};
        VibrationEffect ve = VibrationEffect.createWaveform(patternT, patternA, 0);
        Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        v.vibrate(ve);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button vibrateBtn = findViewById(R.id.btnVibrate);
        vibrateBtn.setOnClickListener(view->vibrate());
    }
}
0

There are 0 best solutions below