如何从联系人中获取手机号码

2023-12-29

我的应用程序要求从联系人列表中选择一个联系人,然后仅获取所选联系人的姓名和手机号码以将其存储在应用程序中,我成功获取了姓名,但如何验证该联系人是否有手机号码(不是一个家)然后得到号码?

如何查看联系人是否有一个或多个手机号码?


在android中,联系人姓名和电话号码保存在不同的ContentProvider中,因此可以从下面的代码中获取contact_id

  cur=cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER +" > 0", null, null);
      cur.moveToFirst();
      while(cur.isAfterLast()==false){
        //    Log.e("Name is:",cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
              Fid=Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)));

              int id=Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)));
              Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null, null, null);

              pCur.moveToFirst();
              while (pCur.isAfterLast()==false) {
                  int idinner=Integer.parseInt(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
                  if(idinner==id){ 

//Add id to Array

                 }
                 pCur.moveToNext();
            }
          cur.moveToNext();
      }

通过这个id你可以获得手机号码和其他详细信息

public String getNo(String[] no){

    String seleContact="";  
//  String[] contactNos=new String[no.length];
    for(int i=0;i<no.length;i++){
        if(no[i].trim().toString().equalsIgnoreCase("")){
            break;
        }
        int id=Integer.parseInt(no[i]);
        //Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = "+id, null, null);
        //cur.moveToFirst();

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);
        pCur.moveToFirst();

        while(pCur.isAfterLast()==false){
                 if(Integer.parseInt(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)))==(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)){
    //               String uname=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).toString();
                     String tempMoNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     Log.e("Activity result selelength is",String.valueOf(seleContact.length()));
                     String[] temp=tempMoNo.split("-");
                     String MoNo="";
                     int le=temp.length;
                     for(int j=0;j<le;j++){
                         MoNo +=temp[j];
                     }

                     if (seleContact.length() > 0) {
                            seleContact += "," + (MoNo);
                        } else {
                            seleContact += (MoNo);
                            }            
                 }
            pCur.moveToNext();
             }
        pCur.close();
    }

    return seleContact;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从联系人中获取手机号码 的相关文章

随机推荐