« Profiling main screen - Expense Report (6) | Main | JPDialer is ready for download »

19 December 2008

Making a phone call programmatically from Android - JPDialer

Recently I got a T-Mobile G1, Google Android mobile phone. For my study of Android platform, I started to port my MIDP applications to Android. The first target was Dialer, which was so small that it should be easily ported. By the way, as the name “Dialer” is used by Android’s standard telephony application, I named Android version of my application “JPDialer”.

In order to make a phone call to a particular phone number programmatically from an Android mobile phone, Context.startActivity() method is used with specifying an Intent.ACTION_DIAL or Intent.ACTION_CALL, and a phone number. If you would use the latter Intent, you need to add CALL_PHONE permission to your application’s manifest. The following code snippet can place a phone call:

String phoneNumber = ...;

Uri uri = Uri.fromParts("tel", phoneNumber, null);

Intent callIntent = new Intent(Intent.ACTION_CALL, uri);


It is convenient if the user can pick a phone number from a list of contacts. To do this, you can use Intent.ACTION_GET_CONTENT with specifying a MIME type Contacts.Phones.CONTENT_ITEM_TYPE, and call Activity.startActivityForResult() method. The following code snippet works for this purpose:

Intent lookupIntent = new Intent(Intent.ACTION_GET_CONTENT);


startActivityForResult(lookupIntent, 0);

After the user picks up a phone number from the list, Activity.onActivityResult() event handler will be called. In its implementation, you can retrieve the phone number from contacts database by using a URI that has been passed in the data parameter. In contrast with MIDP case that the platform provides a way to look up contacts, the given data is not a phone number but a URI that points to an entry in the contacts database. Therefore, you need explicitly to access the contacts database to retrieve the phone number. The following code snippet gives you an example to do it:

Uri uri = data.getData();

Cursor cursor = managedQuery(uri, null, null, null, null);


String phoneNumber = cursor.getString(cursor.getColumnIndex(Contacts.PhonesColumns.NUMBER));

Since you access the contacts database, you need to add READ_CONTACTS permission to your application’s manifest.

I tried them and confirmed that they worked with T-Mobile G1.


« Profiling main screen - Expense Report (6) | Main | JPDialer is ready for download »





TrackBack URL for this entry:

Listed below are links to weblogs that reference Making a phone call programmatically from Android - JPDialer:

« Profiling main screen - Expense Report (6) | Main | JPDialer is ready for download »