Android跳转到通讯录获取用户名称和手机号码的实现思路

Android跳转到通讯录获取用户名称和手机号码的实现思路


这篇文章主要介绍了Android跳转到通讯录获取用户名称和手机号码的实现思路,当用户点击跳转到通讯录界面 并取通讯录姓名和手机号码 ,实现代码简单易懂,非常不错感兴趣的朋友一起看看吧

效果图如下所示:

先给大家说下实现android 跳转到通讯录的实现思路:

1.点击跳转到通讯录界面

2.获取通讯录姓名和手机号码

3.回调显示姓名和手机号码

1首先是跳转到通讯录界面

?
1
2
3
Uri uri = Uri.parse( "content://contacts/people" );
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, 0 );

通过设置通讯录url跳转,可以看到我们用回调函数实现

2.回调函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
  * 跳转联系人列表的回调函数
  * */
  @Override
  protected void onActivityResult( int requestCode, int resultCode, Intent data) {
   switch (requestCode){
    case 0 :
     if (data== null )
     {
      return ;
     }
     //处理返回的data,获取选择的联系人信息
     Uri uri=data.getData();
     String[] contacts=getPhoneContacts(uri);
     et_name.setText(contacts[ 0 ]);
     et_tele.setText(contacts[ 1 ]);
     break ;
   }
   super .onActivityResult(requestCode, resultCode, data);
  }

其中getPhoneContacts(uri)方法,因为手机的联系人和手机号并不再同一个数据库中,所以我们需要分别做处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private String[] getPhoneContacts(Uri uri){
   String[] contact= new String[ 2 ];
   //得到ContentResolver对象
   ContentResolver cr = getContentResolver();
   //取得电话本中开始一项的光标
   Cursor cursor=cr.query(uri, null , null , null , null );
   if (cursor!= null )
   {
    cursor.moveToFirst();
    //取得联系人姓名
    int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    contact[ 0 ]=cursor.getString(nameFieldColumnIndex);
    //取得电话号码
    String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null ,
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null , null );
    if (phone != null ){
     phone.moveToFirst();
     contact[ 1 ] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phone.close();
    cursor.close();
   }
   else
   {
    return null ;
   }
   return contact;
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值