sábado, 27 de octubre de 2012

How to make Android Vibrate?


This is an example of how to make vibration with Android

Making Android phones vibrate is a good way to provide haptic feedback to users or to interact with users even when phone volume is low. This can ensure a better user experience and therefore increase the perceived integrity of your Android application.

The Vibrator Documentation describes the interface for making an Android phone vibrate. With this interface, you can cause an Android phone to vibrate in one of the following ways:

Vibrate for a given length of time
Vibrate in a given pattern
Vibrate repeatedly until cancelled
Below are examples of these three vibrating methods.

IMPORTANT: First, Grant Vibration Permissions
Before you start adding the code necessary to cause your application to vibrate, you must first notify Android that your application expects to have permission to use the Vibrator. If you do not do this, you will receive a Force Close. Nobody likes encountering a Force Close; Do not forget this important first step!

Add the uses-permission line to your Manifest.xml file, outside of the block.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
    <uses-permission android:name="android.permission.VIBRATE"/>
    <application android:label="...">
        ...
    </application>
</manifest>


Example 1: Vibrate for a Given Length of Time

This example is useful when a user touches your application and you would like to provide haptic feedback. This is the simplest method of vibration. I like 50 milliseconds as a good single-touch-feedback vibrate. You can experiment with different lengths (on your physical phone) to decide how long to make your vibration.

WARNING: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Vibrate for 300 milliseconds
v.vibrate(300);

Example 2: Vibrate in a Given Pattern

This method of vibration is useful when you need to provide a user with a one-time notification, such as receiving a text message. In this example, I have created a one-time vibration notification in the Morse Code SOS pattern.

WARNING: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// This example will cause the phone to vibrate "SOS" in Morse Code
// In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash"
// There are pauses to separate dots/dashes, letters, and words
// The following numbers represent millisecond lengths
int dot = 200;      // Length of a Morse Code "dot" in milliseconds
int dash = 500;     // Length of a Morse Code "dash" in milliseconds
int short_gap = 200;    // Length of Gap Between dots/dashes
int medium_gap = 500;   // Length of Gap Between Letters
int long_gap = 1000;    // Length of Gap Between Words
long[] pattern = {
    0,  // Start immediately
    dot, short_gap, dot, short_gap, dot,    // s
    medium_gap,
    dash, short_gap, dash, short_gap, dash, // o
    medium_gap,
    dot, short_gap, dot, short_gap, dot,    // s
    long_gap
};

// Only perform this pattern one time (-1 means "do not repeat")
v.vibrate(pattern, -1);

Example 3: Vibrate Repeatedly Until Cancelled

This method of vibration is useful when you need to notify the user of something that requires more immediate action, such as an incoming phone call. You can repeat a given pattern until the Vibrator is cancelled, either by the system or manually by your Android application.

WARNING: The below example will cease vibrating when the screen times out.
WARNING: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start immediately
// Vibrate for 200 milliseconds
// Sleep for 500 milliseconds
long[] pattern = { 0, 200, 500 };

// The "0" means to repeat the pattern starting at the beginning
//  WARNING : If you start at the wrong index (e.g., 1) then your pattern will be off --
// You will vibrate for your pause times and pause for your vibrate times !
v.vibrate(pattern, 0);
In another part of your code, you can handle turning off the vibrator as shown below:

view sourceprint?
// Stop the Vibrator in the middle of whatever it is doing
//  WARNING : Do *not* do this immediately after calling .vibrate().
// Otherwise, it may not have time to even begin vibrating!
v.cancel();

Source: Android | Konreu

No hay comentarios:

Publicar un comentario