This code snippet queries all the contacts from the android device and sort those contacts
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ContactActivity extends Activity {
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
ListView nameList;
private DBAdapter db;
String contact="";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle("Wait....");
setContentView(R.layout.deleteaccountlist);
db=new DBAdapter(this);
db.open();
nameList=(ListView)findViewById(R.id.listView2);
android.telephony.TelephonyManager mng=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
new QueryContacts().execute();
nameList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
HashMap map=contactData.get(position);
db.insertSMSContact((String)map.get("name"),(String)map.get("number"));
db.insertSpeechDetails((String)map.get("number"), 0);
setResult(Activity.RESULT_OK);
finish();
}
});
}
class QueryContacts extends AsyncTask<Void,Void,Void> {
Dialog dialog;
@Override
protected void onPreExecute( ) {
dialog=ProgressDialog.show(ContactActivity.this,"Please wait...", "Loading the contacts..",true, true);
contactData.clear();
}
@Override
protected void onPostExecute(Void result) {
SimpleAdapter contactsAdapter=new SimpleAdapter(ContactActivity.this,contactData,R.layout.contactview,new String[]{"name","number"},new int[]{R.id.contactName,R.id.contactNumber});
nameList.setAdapter(contactsAdapter);
dialog.cancel();
setTitle("Select Contact:");
}
protected void onProgressUpdate(Void... values) {
}
@SuppressWarnings("unchecked")
@Override
protected Void doInBackground(Void... arg0) {
ContentResolver cr = getContentResolver();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
try{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
}
phones.close();
}
}catch(Exception e){}
}
/*Cursor cur = cr.query(People.CONTENT_URI,null, null, null, null);
while (cur.moveToNext()) {
try{
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
// if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI, null,Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
HashMap<String,String> map=new HashMap<String,String>();
String phoneNumber=pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
contact=contact+":"+name;
contact=contact+":"+phoneNumber;
}
//}
}catch(Exception e){e.printStackTrace();}
}
/*
while (cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex( People.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
if(phoneNumber!=null)
contactData.add(map);
}*/
Collections.sort(contactData, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
HashMap map1=(HashMap)o1;
HashMap map2=(HashMap)o2;
String s1=(String)map1.get("name");
String s2=(String)map2.get("name");
return s1.compareTo(s2);
}
});
/*
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
}
phones.close();
}
}
Collections.sort(contactData, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
HashMap map1=(HashMap)o1;
HashMap map2=(HashMap)o2;
String s1=(String)map1.get("name");
String s2=(String)map2.get("name");
return s1.compareTo(s2);
}
});*/
return null;
}
}
}
Also declare the permission in the android manifest file
<uses-permission android:name="android.permission.READ_CONTACTS"/>
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ContactActivity extends Activity {
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
ListView nameList;
private DBAdapter db;
String contact="";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle("Wait....");
setContentView(R.layout.deleteaccountlist);
db=new DBAdapter(this);
db.open();
nameList=(ListView)findViewById(R.id.listView2);
android.telephony.TelephonyManager mng=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
new QueryContacts().execute();
nameList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
HashMap map=contactData.get(position);
db.insertSMSContact((String)map.get("name"),(String)map.get("number"));
db.insertSpeechDetails((String)map.get("number"), 0);
setResult(Activity.RESULT_OK);
finish();
}
});
}
class QueryContacts extends AsyncTask<Void,Void,Void> {
Dialog dialog;
@Override
protected void onPreExecute( ) {
dialog=ProgressDialog.show(ContactActivity.this,"Please wait...", "Loading the contacts..",true, true);
contactData.clear();
}
@Override
protected void onPostExecute(Void result) {
SimpleAdapter contactsAdapter=new SimpleAdapter(ContactActivity.this,contactData,R.layout.contactview,new String[]{"name","number"},new int[]{R.id.contactName,R.id.contactNumber});
nameList.setAdapter(contactsAdapter);
dialog.cancel();
setTitle("Select Contact:");
}
protected void onProgressUpdate(Void... values) {
}
@SuppressWarnings("unchecked")
@Override
protected Void doInBackground(Void... arg0) {
ContentResolver cr = getContentResolver();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
try{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
}
phones.close();
}
}catch(Exception e){}
}
/*Cursor cur = cr.query(People.CONTENT_URI,null, null, null, null);
while (cur.moveToNext()) {
try{
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
// if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI, null,Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
HashMap<String,String> map=new HashMap<String,String>();
String phoneNumber=pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
contact=contact+":"+name;
contact=contact+":"+phoneNumber;
}
//}
}catch(Exception e){e.printStackTrace();}
}
/*
while (cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex( People.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
if(phoneNumber!=null)
contactData.add(map);
}*/
Collections.sort(contactData, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
HashMap map1=(HashMap)o1;
HashMap map2=(HashMap)o2;
String s1=(String)map1.get("name");
String s2=(String)map2.get("name");
return s1.compareTo(s2);
}
});
/*
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
}
phones.close();
}
}
Collections.sort(contactData, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
HashMap map1=(HashMap)o1;
HashMap map2=(HashMap)o2;
String s1=(String)map1.get("name");
String s2=(String)map2.get("name");
return s1.compareTo(s2);
}
});*/
return null;
}
}
}
Also declare the permission in the android manifest file
<uses-permission android:name="android.permission.READ_CONTACTS"/>
No comments:
Post a Comment