调用android自带的联系人
在android程序的开发中,经常要实现的一个功能是调用系统自带的联系人界面,做到选择人之后,取得相应的名称和号码。而android2.0前后实现方式上也有一些差别,主要是2.0版本后联系人API发生了变化,由原来的Contacts变成了ContactsContract。
一 2.0之前取联系人列表的方式:
声明意图方式一
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(Contacts.People.CONTENT_URI);
startActivityForResult(intent, );
PICK_CONTACT
声明意图方式二
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("Contacts.People.CONTENT_TYPE");//vnd.android.cursor.dir/person
startActivityForResult(intent, );
PICK_CONTACT
实现onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case :
PICK_CONTACT
if (data == null) {
return;
}
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow(Phones.NUMBER));
Log.d(TAG, "number" + number);
mContactText.setText(number);
mContactText.setSelection(number.length());
break;
default:
break;
}
}
二 2.0之后取联系人列表的方式:
声明意图方式一
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
声明意图方式二
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);//vnd.android.cursor.dir/contact
startActivityForResult(intent, PICK_CONTACT);
实现onActivityResult
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String phoneNumber = null;
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones=
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
}
break;
}
}
相关推荐
pigsmall 2020-11-19
SXIAOYI 2020-09-16
linuxprobe0 2013-04-01
83560193 2013-06-25
kidneybeans 2013-06-17
加菲猫园 2013-06-16
年轻就要对味 2014-07-11
稻草人的高粱地 2014-07-02
smilebestSun 2014-06-12
xusong 2014-05-17
zfszhangyuan 2013-09-12
80183053 2013-09-12
gaozhlzh 2013-09-11
87453169 2014-01-17
Tom天天 2013-07-12
Ladyseven 2020-07-25
whileinsist 2020-06-24
gufudhn 2020-06-12