通过Contact_name对移动设备进行扩展

时间:2011-07-15 12:47:06

标签: android

我是android开发的新手。我想从联系人名称中查找联系人号码。我使用android 2.1.my联系人名称在微调器中轻松回复,我想从联系人名称获取电话号码我该怎么办请帮助我

提前致谢

此致 Abhishek Soni

1 个答案:

答案 0 :(得分:3)

在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){ 
             }
             pCur.moveToNext();
        }
      cur.moveToNext();
  } 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){ 
             }
             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;
}

使用此方法以字符串格式传递联系人ID。