Controlling vibra settings in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Code doesn't seem to work on Nokia 701 or Nokia Asha 305

Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131, Nokia N81, Nokia 701, Nokia Asha 305
Created: IlGolub (28 Nov 2008)
Reviewed: mtilli (02 Jul 2012)
Last edited: hamishwillee (13 Sep 2013)



This article explains how to use device's vibration function.

To start vibrating device a call to vibrate()-method of Display-instance is used.

Duration of the vibration is given as a parameter. Note that this method returns immediately, so it doesn't wait till vibration is fnished.

The Thread.sleep() method is used for waiting until the end of the vibration.

Source file: ControllingVibraMIDlet.java

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

public class ControllingVibraMIDlet extends MIDlet implements CommandListener {
* Command for calling vibration

private Command controllingVibraCommand;
* exit midlet command

private Command exitCommand;
private Form mainForm;
* The ControllingVibraMIDlet constructor.

public ControllingVibraMIDlet() {
* Performs an action assigned to the Mobile Device - MIDlet Started point.

public void startMIDlet() {
* Switches a current displayable in a display. The Display instance is
* taken from getDisplay() method. This method is used by all actions
* in the design for switching displayable.
* @param nextDisplayable the Displayable to be set

public void switchDisplayable(Displayable nextDisplayable) {
Display display = getDisplay();
* From CommandListener.
* Called by a system to indicated that a command has been invoked on a
* particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked

public void commandAction(Command command, Displayable displayable) {
if (displayable == mainForm) {
if (command == exitCommand) {
} else if(command == controllingVibraCommand) {
try {
//displayObject.vibrate(int) Requests operation of the
//device's vibration.
//This method switches on the vibration for the requested
//duration, or switches it off if the requested duration
//is zero. This method returns immediately; that is, it
//must not block the caller while the vibration is running.
//that's why next we call Thread.sleep() function
mainForm.append("1000 milliseconds duration");
mainForm.append("700 milliseconds duration");
mainForm.append("starting with 500 milliseconds duration" +
" but we will stop vibration after first 300 " +
"milliseconds using 0 duration");
} catch (InterruptedException ex) {
* Returns an initiliazed instance of mainForm component.
* @return the initialized component instance

public Form getForm() {
if (mainForm == null) {
mainForm = new Form("Vibra test!");
exitCommand = new Command("Exit", Command.EXIT, 0);
controllingVibraCommand = new Command("Vibra", Command.SCREEN, 0);
return mainForm;
* Returns a display instance.
* @return the display instance.

public Display getDisplay () {
return Display.getDisplay(this);
* Exits MIDlet.

public void exitMIDlet() {
* From MIDlet.
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and
* initialize/starts or resumes the MIDlet.

public void startApp() {
* From MIDlet.
* Called when MIDlet is paused.

public void pauseApp() {
//Empty implementation
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally
* terminated and all resources has to be released.

public void destroyApp(boolean unconditional) {
//Empty implementation


This MIDlet implements a simple example for using the device's vibration function.

When the "Vibra"-command has been selected from the menu, the device will vibrate for 1000 milliseconds, and then 700 milliseconds with a small delay between.

Next starts a vibration for 500 milliseconds, but after first 300 milliseconds it is turned off by the duration of vibra being set to 0 milliseconds.

Supplementary material

You can download this MIDlet and source code from here: Controlling Vibra Settings.zip

This page was last modified on 13 September 2013, at 05:44.
94 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.