# Vibrations in Processing for Android

**URL:** <https://discourse.processing.org/t/vibrations-in-processing-for-android/44858>\
**Category:** Processing for Android\
**Created:** [July 29, 2024, 4:28pm UTC](https://discourse.processing.org/t/vibrations-in-processing-for-android/44858 "2024-07-29T16:28:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ripolas](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@ripolas](https://discourse.processing.org/u/ripolas)\
**Post date:** [July 29, 2024, 4:28pm UTC](https://discourse.processing.org/t/vibrations-in-processing-for-android/44858/1 "2024-07-29T16:28:55Z")

</div>

So I’ve been trying to make my phone vibrate when the screen is clicked, but it’s not working. I’ve checked the sketch premissions too. Thank you for your help. The code that isn’t working

```auto
import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.os.VibratorManager;

Vibrator vibrator;

void setup() {
  fullScreen();
  
  VibratorManager vibratorManager = (VibratorManager) getActivity().getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
  vibrator = vibratorManager.getDefaultVibrator();
  println("vibrating");
  vibrator.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
}

void draw() {
  background(0);
  fill(255);
  textAlign(CENTER, CENTER);
  text("Touch the screen to vibrate", width / 2, height / 2);
}

void touchStarted() {
  vibrator.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
  println("vibrating");
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 29, 2024, 7:56pm UTC](https://discourse.processing.org/t/vibrations-in-processing-for-android/44858/2 "2024-07-29T19:56:16Z")

</div>

The following source code will cause an old Nexus phone to vibrate. It has no effect on a tablet because I’m not sure they have that feature. Make sure ‘vibrate on touch’ is turned on in the phone’s settings and the sketch’s permissions is set to ‘Vibrate’. You may not need all those imports; I didn’t test which ones are used. Note that VIBRATOR\_SERVICE is marked as deprecated, but it still works.

```auto
//https://stackoverflow.com/questions/45605083/android-vibrate-is-deprecated-how-to-use-vibrationeffect-in-android-api-26
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.VibrationAttributes;
import android.os.VibrationAttributes.Builder;
import android.os.Vibrator;
import android.os.VibratorManager;
import android.os.Build;

void shakeIt() {
    if (Build.VERSION.SDK_INT >= 26) {
      println("sdk > 26");
        ((Vibrator) this.getContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150,10));
    } else {
      println("sdk < 26");
        ((Vibrator) this.getContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
    }
}

void setup() {
  fullScreen();
}

void draw() {
  background(0);
  fill(255);
  textSize(100);
  textAlign(CENTER, CENTER);
  text("Touch the screen to vibrate", width / 2, height / 2);
}

void touchStarted(){
  shakeIt();
}

```
