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!

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!

Create your own Apple iPhone Clock App


Takeaway: Continuing his basic iOS development tutorial, Todd Moore shows us how to create a simple iPhone Clock App.
In this post, we will make an iPhone Clock App which displays the current time on the screen. This is a continuation from our first Hello World App. Open up the previous Hello project and I’ll show you how to get programmatic access to the label so that the text can be modified within our ViewController class. In order to do this you will need to connect the label in interface builder to a property of the ViewController class.

Automatically sign up for TechRepublic's App Builder newsletter!

Say goodbye

Open ViewControler.xib and use the toolbar to show the assistant editor. (Figure A)

Figure A

The secondary editor is displayed next to the ViewController interface and should automatically open the header file for the ViewController class.
Hold down the control key and then click and drag from the label to inside the view controller header file as shown in Figure B.

Figure B

Once the arrow reaches inside the interface definition you can let go to receive an Outlet connection popup. Set the name of the control to “label” and keep everything else the same as shown Figure C.

Figure C

Xcode will automatically add a property definition for you inside of the header file and also stub out an implementation in the source file. Open up the ViewController.m source file and verify that you can manipulate the label by changing its text to “Bye” in the viewDidLoad method. (Figure D)

Figure D

Run the app (Figure E) and it should now say Bye instead of Hello.

Figure D

Start the clock

Now let’s do something a little more interesting and display the current system time. Add the following code (Figure E) into the source file to create an updateLabel method:

Figure E

This method will allocate an NSDateFormatter object with the date format of “hh:mm:ss” and then use that with the current date to set the label. The updateTime method is then called again using performSelector with a delay of one second. This makes sure the label keeps getting updated and doesn’t just get set one time.
The last step (Figure F) is to call updateTime from the viewDidLoad method:

Figure F

Build and run and you should now see (Figure G) a full screen clock being updated every second.

Figure G

Your new clock app is a lot easier to read than the time contained in the status bar along the top. In my next tutorial, I’ll show you how to take this code and turn it into a stopwatch app.

Six Xperia Android smartphones unveiled in Southeast Asia


Takeaway: Sony Mobile is demonstrating a big commitment to Android in Southeast Asia with its six new Xperia smartphones.
Sony Mobile Communications showcased its range of Xperia smartphones in Southeast Asia at an event in late June 2012. The new smartphones in the company’s Xperia lineup (which already includes Xperia P, Xperia U, and Xperia sola smartphones) are Xperia ion, Xperia go, Xperia acro S, Xperia neo L, Xperia miro, and Xperia tipo (Figure A), all of which run on Android 4.0.
Figure A
From left to right: Xperia ion, Xperia go, Xperia acro S, Xperia neo L, Xperia miro, Xperia tipo. (Photo: Paul Mah)
The impressive-looking phones are all positioned at slightly different niches. Based on information from Sony, here are brief highlights about each device’s capabilities.

Xperia ion

The flagship Xperia ion smartphone offers a large 4.6-inch HD resolution screen (1280×720) with rear (1080p) and front (720p) camera for HD recording. It is powered by a 1.5GHz dual core processor and a 12MP rear camera.

Xperia go

The Xperia go comes with a 3.5-inch display and a 5MP fast capture camera. This phone has an IP67 rating, which is the highest level of dust and water resistance found in a smartphone to date. It has a scratch-resistant mineral glass display with wet finger tracking; you can see photos of the Xperia go’s water resistance tested in Figure B and Figure C. The phone will be available in Indonesia, Malaysia, and Singapore in Q3 2012.
Figure B
The Xperia go held under the water of a miniature fountain. (Photo: Paul Mah)
Figure C
The Xperia go (Photo: Paul Mah)

Xperia acro S

The Xperia acro S has a 4.3-inch HD display and a 12MP camera. Armed with IP55 and IP57 ratings for dust and water resistance, it is also PlayStation certified. It will be available in Singapore and Indonesia in Q3 2012.

Xperia neo L

The Xperia neo L comes with a 4-inch display and is powered by a 1GHz processor with Adreno 205 graphics processor. Running on Android 4.0, it incorporates a front-facing camera for video chat via Skype and a 5MP back camera with the ability to do 720p HD video capture. The Xperia neo L is currently available at telecommunications provider SingTel, Sony stores, Sony centers, and Xperia concept stores for a recommended retail price of SG$388 (~US$307). The device’s colors are black and white.

Xperia miro

The Xperia miro has a 3.5-inch display and is marketed as deeply integrated with Facebook. Illuminations that can be customized to various colors can be configured to alert users to incoming messages and social updates. The Xperia miro will be available in Indonesia, Malaysia and Singapore in Q3 2012.

Xperia tipo

The Xperia tipo is billed as simple, easy to use, and well suited for users purchasing their first smartphone. Fronted by a 3.2-inch scratch-resistant mineral glass display, it has a built-in application for monitoring data usage. Sony says the tipo will also be available in a dual SIM model, though that will only be available in Singapore and Malaysia. The Xperia tipo will be available in Q3 2012.