Pages

Minggu, 05 Agustus 2012

Spice up your Android apps with Google’s vibrator service


Takeaway: Learn how to use Android’s vibrator service to improve your app’s UI and cause the device to shake and implement repeating, meaningful patterns.
If you want to improve the user experience of your app by providing haptic feedback but are too afraid to google “Android Vibrator,” you’ve come to the right place.
Since version 1.0 of Android, the SDK has included a system vibrator service that gives creative developers the ability to cause the device to shake and implement repeating, meaningful patterns. The results can be pretty dramatic. Follow along this tutorial on using the Android vibrator service, or download the entire project and import it directly into your Eclipse workspace.
1. Create a new Android project. Target Android 1.6 or greater. Be sure to rename the startup activity to Main.java.
2. Android’s vibrator service is hardware, so we need to declare a special permission in the manifest to access it.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.authorwjf.vibrator"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. In our /res/layout folder, we need to add a single button for the purposes of our demonstration.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_margin="10dip"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Vibrator!" />
<Button
android:layout_margin="10dip"
android:layout_gravity="center"
android:id ="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON"/>
</LinearLayout>
4. We can finish up in the Main.java file in our /src folder. We declare our class-wide variables, wire up our button, and instantiate the vibrator service.
Main.java
package com.authorwjf.vibrator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
Vibrator mVibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.toggle).setOnClickListener(this);
mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
}
}
5. Add the on click handler. Note the parameters for the vibrate method. The first parameter is an array of longs. The first long in that array is the number of milliseconds to pause before beginning the vibrations. What follows are a series of alternating on and off vibrations, though in our case we simply vibrate for 200 milliseconds, and then turn it off.  The final parameter is the repeat index, so in other words we repeat or sequence from position 0 of the array. A negative value for the final parameter disables the repeat functionality, so the pattern only executes once. For our purposes we’ve essentially created a continual vibration by repeating the entire thing and using a zero millisecond pause on both ends of the array.
@Override
public void onClick(View v) {
Button b = (Button)v;
if (b.getText().toString().equalsIgnoreCase("ON")) {
b.setText("OFF");
mVibrator.vibrate(new long[]{ 0, 200, 0 }, 0);
} else {
b.setText("ON");
mVibrator.cancel();
}
}
6. We need to override the on destroy method; this prevents the vibrator from accidently getting left on when the app is pushed to the background or the phone is rotated. Having a constantly vibrating phone would be hard on the battery and embarrassing!
@Override
public void onDestroy() {
if (mVibrator!= null)
mVibrator.cancel();
super.onDestroy();
}
We are ready to load the code and give it a try.
Figure A
While the code executes on the emulator (Figure A), the emulator doesn’t vibrate so you sort of miss out on that little thrill when the device nearly jumps out of your hand. Okay, maybe that’s an exaggeration, but there are some pretty interesting uses for the vibrator service. From alerting users when the volume is low to implementing a silent version of Morse code, the vibrator service allows the developer to capture a powerful human sense in the user experience. Just be sure to use the term haptics and not vibrator when your boss asks you what all the racket was coming from your cube yesterday!