亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android跳轉(zhuǎn)到通訊錄獲取用戶(hù)名稱(chēng)和手機(jī)號(hào)碼的實(shí)現(xiàn)思路

 更新時(shí)間:2016年10月17日 10:40:20   作者:小哥在江湖  
這篇文章主要介紹了Android跳轉(zhuǎn)到通訊錄獲取用戶(hù)名稱(chēng)和手機(jī)號(hào)碼的實(shí)現(xiàn)思路,當(dāng)用戶(hù)點(diǎn)擊跳轉(zhuǎn)到通訊錄界面 并取通訊錄姓名和手機(jī)號(hào)碼 ,實(shí)現(xiàn)代碼簡(jiǎn)單易懂,非常不錯(cuò)感興趣的朋友一起看看吧

效果圖如下所示:

先給大家說(shuō)下實(shí)現(xiàn)android 跳轉(zhuǎn)到通訊錄的實(shí)現(xiàn)思路:

1.點(diǎn)擊跳轉(zhuǎn)到通訊錄界面

2.獲取通訊錄姓名和手機(jī)號(hào)碼

3.回調(diào)顯示姓名和手機(jī)號(hào)碼

1首先是跳轉(zhuǎn)到通訊錄界面

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

通過(guò)設(shè)置通訊錄url跳轉(zhuǎn),可以看到我們用回調(diào)函數(shù)實(shí)現(xiàn)

2.回調(diào)函數(shù)

/*
 * 跳轉(zhuǎn)聯(lián)系人列表的回調(diào)函數(shù)
 * */
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode){
   case 0:
    if(data==null)
    {
     return;
    }
    //處理返回的data,獲取選擇的聯(lián)系人信息
    Uri uri=data.getData();
    String[] contacts=getPhoneContacts(uri);
    et_name.setText(contacts[0]);
    et_tele.setText(contacts[1]);
    break;
  }
  super.onActivityResult(requestCode, resultCode, data);
 }

其中g(shù)etPhoneContacts(uri)方法,因?yàn)槭謾C(jī)的聯(lián)系人和手機(jī)號(hào)并不再同一個(gè)數(shù)據(jù)庫(kù)中,所以我們需要分別做處理

private String[] getPhoneContacts(Uri uri){
  String[] contact=new String[2];
  //得到ContentResolver對(duì)象
  ContentResolver cr = getContentResolver();
  //取得電話本中開(kāi)始一項(xiàng)的光標(biāo)
  Cursor cursor=cr.query(uri,null,null,null,null);
  if(cursor!=null)
  {
   cursor.moveToFirst();
   //取得聯(lián)系人姓名
   int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
   contact[0]=cursor.getString(nameFieldColumnIndex);
   //取得電話號(hào)碼
   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;
 }

3.加權(quán)限

<!--獲取通訊錄權(quán)限-->
<uses-permission Android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

以上所述是小編給大家介紹的Android跳轉(zhuǎn)到通訊錄獲取用戶(hù)名稱(chēng)和手機(jī)號(hào)碼的實(shí)現(xiàn)思路,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論