开发者

Featching mobile no by Contact_name

I am novice in android development.i want to retrive contact number from contact name.I use android 2.1.my contact name retrive easily in spinner and i want to get phone n开发者_JS百科o from contact name how can i do that please help me.

Thanks in advance

Regards Abhishek Soni


In android Contact name and number is save in different ContentProvider so for take contact_id from below code

 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();
  }

and than this id you can get mobile number and other details

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;
}

use this method pass contact id in string format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜