In this tutorial we simply display the name of all Contacts which are saved at our phone .
package com.example.contactslist;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.app.ListActivity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.Toast;
public class Contacts extends ListActivity {
private String name;
private String phone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Uri all=ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cursor = managedQuery(all, projection, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
//
String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhone=cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Toast.makeText(getApplicationContext(), "name..."+name, Toast.LENGTH_SHORT).show();
}
}
}
Add the Read contacts permission to manifest fileimport android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.app.ListActivity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.Toast;
public class Contacts extends ListActivity {
private String name;
private String phone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Uri all=ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cursor = managedQuery(all, projection, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
//
String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhone=cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Toast.makeText(getApplicationContext(), "name..."+name, Toast.LENGTH_SHORT).show();
}
}
}
0 Comments