Wednesday, March 31, 2010

Creating a MediaPlayer in a Service

Create a class which extends Service, in that in onCreate(), create the mediaPlayer and if we need, we can provide a notification also, to notify the user that a background service is being activated.

In onStart(), start(), the mediaplayer also, set looping equals true. So that the mediaplayer will play continously

The complete code is

public class AudioService extends Service {

Mediaplayer player;
public void onCreate() {
player = MediaPlayer.create(this, R.raw.mj);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
nOTIFICATION NOTIFICATION = new Notification(0, "Player started", System.currentTimeMillis());
nm.notify(0, notification);
} catch(Exception e){

public void onStart(Intent intent, int startId) {

public IBinder onBind(Intent intent) {
return null;

Updating List in Android

If we need to update list in android, then according to adapter, we need to modify the code.

For ListAdapter, after updating the arrayList, we need to call notifyDataSetChanged().
For CursorAdapter, after updating the cursor, we need to call requery().

Handling extensive operations in UI thread

If we need to do some extensive operations in UI thread, then create a new thread and do in that thread, so that in UI thread, we can show something else, like progressBar or manually create an animation and can show.

Creating two different apk from a single Application

In android, in some cases, we need to create two different apk from a single application. In that case, just change the package name in manifest file. Then two different apk will get created.

Tuesday, March 30, 2010

Rotating a view through RotateAnimation in Android

For rotating a view,

RotateAniamtion rAnim = new RotateAnimation(0,360, w/2, h/2);
rAnim.setInterpolator(new LinearInterpolator());

List of Interpolators in Android are as follows


Friday, March 26, 2010

Checking Network Connection In Android

For checking connection status in Android, first we need to get an instance of ConnectionManager using getSystemService(). The syntax is as,

ConnectionManager connManager = (ConnectionManager)getSystemService(Context. CONNECTIVITY_SERVICE);

if ( connManager .getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || connManager .getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ) {
text.setText("hey your online!!!");
//Do something in here when we are connected
} else if ( connManager.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
text.setText("Look your not online");

Wednesday, March 24, 2010

Getting Current location from GPS in Android

We can get the current location from Android phones using

- GPS provider
- Network Provider

For this, first create an instance of location manager.

Create a class which implements the LocationListener

LocationManager locationManager = (LocationManager) getSystemService(Context. LOCATION_SERVICE);

Then in the manifest we need to give the permission as,

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

For GPS provider, we will use this statement,

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

and for getting information from network provider, that means , we want to get the current location when inside a building or where, GPS dont work network is there, then we can use this statement for getting location information as,

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);

Then from onLocationChanged() in the locationListener, we will get the latitude and longitude using getLatitude() and getLongitude()

Enabling and disabling lock Screen in Android

For enabling or disabling lock screen in Android, we need to get the instance of keyBoard Manager, using getSystemService(). The syntax is as,

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);

For locking the screen use,


and for disabling the lock use,


This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.

When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"

Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()

And of course we need to give permission android.permission.DISABLE_KEYGUARD

Monday, March 22, 2010

Map in Android

Earlier when the first SDK of androd released, google map is a part of SDK. Now, its not coming with SDK, its coming as add-on.

First for for displaying google map for android application, we need Google map API Key. For this, using the keytool in the JDK, we need to create the finger-print.

We will get the keyStore from eclipse. The path is


I can show a a sample command for getting the finger print in windows xp is,

C:\Program Files\Java\jre1.5.0_06\bin>keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Acer\.android\debug.keystore" -storepass android -keypass android

Type this command in dos prompt and you will get the finger print. Then using this finger-print, we need to get the map api key from the url

Sign Up

After getting the API key, use this API key to display map. Then at the time of creating Android application, we need to select the target as Google API 4 or whichever version we need. It is importand that you create an AVD with target as Google API level 3 or greater.

Then for displaying map, we need to extend MapActivity.

For displaying the zoomIn comtrols,


For making the map clickable and focusable, do


For animating to a particular point in map use,

MapController mc = mapView.getController;

For making a point to center, use


For setting the zoom level use,


For drawing over map use Overlay or ItemizedOverlay. Here we are discussing about the ItemizedOverlay.

Using this, we can easily put marker in the map.

Create another class called MyItemizedOverlay which extends ItemizedOverlay which overrides two functions createItem() and size().

Then we create an ArrayList of OverlayItem. In addition, we create a function called addItem(), in that we are adding OverlayItem to the list and calling populate().

Now our MyItemizedOverlay class will look like this,

class MyItemizedOverlay extends ItemizedOverlay {

private ArrayList mOverlays = new ArrayList();
Context ctContext;

public MyItemizedOverlay(Drawable defaultMarker, Context context) {
ctContext = context;

public void addItem(OverlayItem overlay) {

protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);

public int size() {
// TODO Auto-generated method stub
return mOverlays.size();

Then, on tapping the marker, to display some information, I will override onTap() function and create a dialog for displaying the details. The onTap() function will look like this.

protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Dialog dialog = new Dialog(context);
return true;

Handler In Android

Handler is a verry useful and powerful component in Android. Some special features of Handler are,

->If handler is created without any parameter, then it will be created in the same thread itself.

->We can pass data to handler using message

I created one application, which is using handler efficiently. In which, an image is blinking for one second.

So if we need to create a seperate thread for handler, first we need to create a thread and create the handler inside it.

Reasons for getting java.lang.VerifyError

"java.lang.VerifyError" are worst. VerifyError means that the class file contains bytecode that is syntactically correct but violates some semantic restriction e.g. a jump target that crosses method boundaries.

Some of the reasons for getting VerifyError are,

-> If the bytecode size of your method exceeds the 64kb limit

-> When the class file gets corrupted in some other way (e.g. through faulty RAM or a failing HD).