本文主要是介绍Android-从通讯录导入某个联系人到页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.点击跳转到通讯录界面
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);//一人多个账号铺开
//Uri uri = Uri.parse("content://contacts/people");//多个账号不铺开,默认选第一个startActivityForResult(intent, 0);
2.获取通讯录姓名和手机号码
@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {//通讯录if (requestCode == 0) {if(data==null) { return; }//处理返回的data,获取选择的联系人信息String[] contacts= new String[2];if (data != null) {Uri uri = data.getData();if (uri != null) {Cursor cursor = getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, null, null, null);while (cursor.moveToNext()) {String number = cursor.getString(0);String name = cursor.getString(1);contacts[0] = name;contacts[1] = number;LogUtils.logd("name:" + contacts[0] + "\nphone:" + contacts[1]);}}}}
3.回调显示姓名和手机号码
4.记得加上权限:
<!--获取通讯录权限-->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
这篇关于Android-从通讯录导入某个联系人到页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!