Thursday, September 24, 2009

Reading Contacts from mobile and calling

Before coming to Contacts, first I will touch a bit on Content provider in Android

Content Provider

ContentProvider in android is mainly used to share data between applications. Android has some built-in ContentProviders. They are


-Contacts
-CallLog
-MediaStore
-Browser
-Settings
-SMS


We store data into contentProvider using contentprovider base class. We will store data in any way, generally, using Sqlite database.

We will retrieve data from contentprovider using a single interface called ContentResolver. We can get a object of contentResolver using getContentResolver().

One main thing we require for retrieving data from content provider is the URI.
To get contact list, to the query(), just pass "People.CONTENT_URI".


Cursor c = getContentResolver().query(People.CONTENT_URI, null, null,
null, null);


We will receive the contact details in a cursor. Its better to use startManagingCursor(c). Because then Android ,itself will take care of the cursor, means when activity is stopped, it will call deActivate() automatically on the given cursor. When activity restarts, it will call requery() and at the time of destroying an activity, all the cursors will be closed automatically.

In this program, I am just showing the name and number of the persons in the contact list. We can display lot of data about contact persons like email, notes, ringtone assigned, photo assigned etc. For displaying data, I am first putting all the data in a listAdapter using,

ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, new String[] {
Phones.NAME, Phones.NUMBER }, new int[] {android.R.id.text1,android.R.id.text2 });

The output will look like this in emulator,





Next part is calling the displayed numbers by just clicking on the list. For that, I am going to use onListItemClick(). To know which item in the list, the user clicked, I am using, listAdapter.getItem(position), will give the item clicked. Intent.ACTION_CALL is used for calling a particular. For dialing a number, we have to
use the Uri "tel:".

Another important thing is, we are going to start a new task(Calling), so we have to add the flag "Intent.FLAG_ACTIVITY_NEW_TASK" to the intent.

When we click on any contact, the dial screen will look like this,



After call ends, when we click back button, it will come back to our application.

The full source code is,


public class AndContacts extends ListActivity {

private ListAdapter listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null,
null, null);

startManagingCursor(c);
listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, new String[] {
Phones.NAME, Phones.NUMBER }, new int[] {
android.R.id.text1, android.R.id.text2 });
setListAdapter(listAdapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = ((Cursor) listAdapter.getItem(position));
long phoneNumber = c.getLong(c.getColumnIndex(People.NUMBER));
String num = "tel:";
num += Long.toString(phoneNumber);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(num));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

No comments:

Post a Comment

Androidians