Android获取用户通讯录上传,获取用户通讯录信息的几种方式 – 热爱改变生活

前两种都是通过 Intent 来获取的

第一种

Intent localIntent = new Intent("android.intent.action.PICK");

localIntent.setType("vnd.android.cursor.dir/phone");

mActivity.startActivityForResult(localIntent, REQUEST_0);

选择了通讯录中的用户之后,会回到 OnActivityResult, 其中 disPlayName 和 phoneNumber 分别就是名字和电话

if (resultCode == RESULT_OK) {

Uri localUri = paramIntent.getData();

if (localUri != null) {

Cursor localCursor = getContentResolver().query(localUri, null, null, null, null);

if (localCursor != null)

while (true) {

if (!localCursor.moveToNext()) {

DialogUtil.showTextDialog(mContext, this.disPlayName + "\n" + this.phoneNumber);

super.onActivityResult(paramInt1, resultCode, paramIntent);

return;

}

int i = localCursor.getColumnIndex("display_name");

if (i >= 0) {

this.disPlayName = localCursor.getString(i);

int j = localCursor.getColumnIndex("data1");

if (j >= 0)

this.phoneNumber = localCursor.getString(j);

}

}

}

}

第二种

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(intent, REQUEST_1);

同样调用到 OnActivityResult

if (resultCode == RESULT_OK) {

Uri uri = paramIntent.getData();

Cursor cursor = getContentResolver().query(uri, null, null, null, null);

cursor.moveToFirst();

String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

String number = getContactNumber(cursor);

DialogUtil.showTextDialog(mContext, contactName + "\n" + number);

}

上面的 getCOntactNumber 方法在下面

private String getContactNumber(Cursor cursor) {

String phoneNumber = null;

int numberCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

if (numberCount > 0) {

int contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Cursor phoneNumberCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactID, null, null);

phoneNumberCursor.moveToFirst();

phoneNumber = phoneNumberCursor.getString(phoneNumberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

phoneNumberCursor.close();

cursor.close();

return phoneNumber;

}

return phoneNumber;

}

第三种

第三种是我写的,通过获得所有联系人的信息,在 dialog 里面显示,然后让用户选择,选择了的会返回给程序,代码不算多,但是也不少,所以就直接下载代码看吧。下载在最下方

记得要添加权限

下载地址:

文件名称:联系人信息获取代码

文件大小:5k适用版本:你看着来

更新日期:2016年5月8日17:31:32作者信息:sumile

本博客只要没有注明“转”,那么均为原创。转载请注明链接:sumile.cn » 获取用户通讯录信息的几种方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值