I'll demonstrate Android get phone contacts example. First create a new Android project in Eclipse . Then in your created xml layout just define the following things:
Just add the following codes to your Activity class:
Hossain Doula
Just add the following codes to your Activity class:
public class ContactFetchActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button contactButton = (Button)findViewById(R.id.button1);
contactButton.setOnClickListener(l);
}
OnClickListener l = new OnClickListener(){
@Override
public void onClick(View arg0) {
try{
Uri contactData = ContactsContract.Contacts.CONTENT_URI;
Cursor contactsCursor = managedQuery(contactData,null, null, null, null);
Log.i("The first one ", ""+ contactsCursor.getCount());
while (contactsCursor.moveToNext()) {
String id = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhoneNumber = contactsCursor.getString(contactsCursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Toast.makeText(ContactFetchActivity.this, Integer.parseInt(hasPhoneNumber) + "", 11).show();
if (Integer.parseInt(hasPhoneNumber) > 0) {
Uri myPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
String phoneNumber = null;
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(ContactFetchActivity.this, phoneNumber , 20).show();
}
Toast.makeText(ContactFetchActivity.this, id + "-----" + name , 20).show();
}
}
}catch(Exception ex){
Toast.makeText(ContactFetchActivity.this, "Problem because " + ex.getMessage(), 10).show();
}
//Uri myPerson = ContentUris.withAppendedId(contentUri, id)
}
};
}
Just fix your imports. And then finally on your AndroidManifest.xml file add the line:Hossain Doula