Android Adventures, part 4: vibrate Processing

Go to Part 3… Part 5…

Over the weekend I had time to dig into more Android / Processing, and decided I should learn how to trigger the phone’s vibration function.

The only thing I found out of the ordinary is that you don’t need to include this code in your AndroidManifest.xml file:

  • <uses-permission android:name="android.permission.VIBRATE" />

But you do need to enable the ‘VIBRATE’ option in the ‘Android -> Sketch Permission’ menu.

In the below sketch, it simply vibrates when you touch the screen.

// Vibrate Android via Processing
// Eric Pavey - - 2010-10-24
// You must enable VIBRATE in Android -> Sketch Permissions menu!!!

// Imports:
import android.content.Context;

// Setup vibration globals:
NotificationManager gNotificationManager;
Notification gNotification;
long[] gVibrate = {0,250,50,125,50,62};

void setup() {
  size(screenWidth, screenHeight, A2D);

void draw() {
  // do nothing...

// Override the parent (super) Activity class:
// States onCreate(), onStart(), and onStop() aren't called by the sketch.  Processing is entered
// at the 'onResume()' state, and exits at the 'onPause()' state, so just override them as needed:

void onResume() {
  // Create our Notification Manager:
  gNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  // Create our Notification that will do the vibration:
  gNotification = new Notification();
  // Set the vibration:
  gNotification.vibrate = gVibrate;

// Override the parent (super) SurfaceView class to detect for touch events:

public boolean surfaceTouchEvent(MotionEvent event) {
  // If user touches the screen, trigger vibration notification:
  gNotificationManager.notify(1, gNotification);
  return super.surfaceTouchEvent(event);

Go to Part 3… Part 5…

Android Adventures, part 3: hardware detection in Processing
Android Adventures, part 5: access the camera in Processing
  1. Thanks a lot for these series of blogs related with processing and android. They are really, really useful since there’s not much information about it. Thanks a lot.

    Do you know where I could find really, really good information explaining orientation, accelerometer, and magnetometer interpretation ? There’s a lot of things, but I need the DEFINITELY tutorial, because I found it difficult to understand.

    Thanks a lot for your good posts.

  2. Glad they help! I think the SensorEvent API docs do a good job covering the specifics:

    And I found this page that seem to lay out how the axes related to the phone (which the api docs do as well):

    • baz
    • April 8th, 2011 8:11am

    These tuts look awesome, thanks for putting them up. I am having a real problem running them though and was hoping you could help. Basically, running 0194 in android mode I am getting

    No library found for import android.content.Context;
    No library found for import;
    iNo library found for import;

    Will they work in the emulator or do I need to run them on an actual device?


  3. First off it sounds like you don’t have the Android SDK installed: There is nothing on disk for it to import so its’ raising those errors. Otherwise it may be an issue of it not finding them… and unfortunately I’m not expert in Java configuration (if this was a Python issue I’d be a lot more helpful…).

    However, if you’re trying to vibrate the emulator, I could see there being problems there πŸ˜‰ All the work I did I did directly on the phone, so I don’t have a lot of experience using the emulator. I know SDK docs give examples of faking your GPS location on the emulator so you can code GPS apps with it, but I’m not sure if the same thing is provided for using other bits of the hardware. My only guess is that you can’t do this kind of stuff on the emulator, but I could be wrong.

    • baz
    • April 10th, 2011 11:40pm

    Hey thanks for the response. I definitely have the SDK installed – projects are compiling and running fine in the emulator from eclipse. It is just the imports from within the processing IDE that are not being found. Must be some path thing though I am a little stumped after lots of googling.

  4. AH, you’re doing it through eclipse. All the work I did was directly from the PDE itself to the phone. I know there is some configuration stuff you need to do with Eclipse, but I’ve never messed with it. Definitely hit up the Processing-Android forms for that one.

    • guu
    • April 28th, 2011 6:53am

    Nice tutorials. Now, what if the phone doesn’t vibrate? I have VIBRATE permission checked, but nothing happend on my phone when I touch the screen.

  5. That’s a fine question. Unfortunately the only hardware I’m familiar with is my phone, so it will be hard to know off hand why yours isn’t working. FYI, I use similar vibration code in an app I made, which you can find on this page:
    If you want, try installing that on your phone, and see if the phone vibrates when the ‘big ball’ it hits the sides of the screen: If it doesn’t, then my guess is that your phone doesn’t support vibration the way I’m implementing it. If it does work, it probably means you somehow made a mistake in the tutorial code.

    • guu
    • April 28th, 2011 7:34am

    It works, but it doesn’t vibrate. I’d come back and drop another line if I find the cause. Thanks!

  6. I don’t know anything about CyanogenMod (just looked it up online), but I’m wondering if that has anything to do with it? Would be great to know whats going on if you figure it out πŸ˜‰

    • james
    • April 26th, 2012 4:45am


    I get a ‘cannot find symbol’ error when I try and run to device any idea why this is?

    I am running mac OSX and processing 2.0a5 trying to run it on android 2.3.3

    • james
    • April 26th, 2012 5:14am

    Ok so I got the code working I had to delete the A2D line and then it compiles fine.

    so size looks like this instead:

    size(screenWidth, screenHeight);

    When I first ran the sketch it wouldn’t vibrate I had to make sure my tablet was not on silent so on your phone/ tablet go to sound and change sound profile to vibrate. Then the sketch works perfectly.

    Thanks for the code!

    • michael
    • June 10th, 2012 3:48am

    Thank you so much for providing those examples. Same as james: I removed the line in setup and everything works fine.

    No problems, just this plain thank you note.

    • Taek Kwon Kim
    • July 6th, 2012 5:35am

    size(screenWidth, screenHeight); ==> size(displayWidth, displayHeight);

    • RandomGuy
    • May 2nd, 2013 1:30am

    import android.view.MotionEvent;

    is required for this – pretty important import or you’re going to get an error when compiling at line 41.

  7. @RandomGuy

    They must have changed the API since I first authored this, wasn’t a requirement back then. Thanks for the tip.

  8. @RandomGuy

    Thaaaaank you so much! :)

    • Sarah
    • May 2nd, 2016 11:48am


    I tried compiling this code, but I got the error “the function getSystemService(String) does not exist”

    • Data
    • May 16th, 2016 10:03am

    I got this problems:

    – SurfaceTouchEvent(MotionEvent) does not exist
    – The function onResume() does not exist
    – The function getSystemService() does not exist

    I’m using Processing 3.0b5 πŸ˜‰

  9. The code I have listed is pretty old now, it’s possible the api has changed since then.

  1. February 12th, 2011