/**
* 读取联系人信息
* @author Administrator yangchao
*
*/
public class MainActivity extends Activity
{
public static final String TAG = "yangchao";
private ListView lvContent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvContent = (ListView) findViewById(R.id.lvContent);
List
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name", "number", "email"}, new int[]{R.id.tvName, R.id.tvNumber, R.id.tvEmail});
lvContent.setAdapter(adapter);
}
}
---------------------------------------------------------------------------------------------------------------
public class ContactService
{
private Context context;
public ContactService(Context context)
{
this.context = context;
}
public List
{
List
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.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));
HashMap
map.put("name", name);
Cursor phones = contentResolver.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));
map.put("number", phoneNumber);
Log.i("RongActivity", "phoneNumber=" + phoneNumber);
}
phones.close();
Cursor emails = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
+ contactId, null, null);
while (emails.moveToNext())
{
// This would allow you get several email addresses
String emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
map.put("email", emailAddress);
Log.i("RongActivity", "emailAddress=" + emailAddress);
}
data.add(map);
emails.close();
}
cursor.close();
return data;
}
}