开发者

How to pick contact number from phone-book of android in to my application?

开发者 https://www.devze.com 2023-03-31 03:28 出处:网络
all i want to take number from phonebook of android in my application database.. i have tried it with below code but here am getting name of person instead i want number from phonebook and want to sto

all i want to take number from phonebook of android in my application database.. i have tried it with below code but here am getting name of person instead i want number from phonebook and want to store it in my database..how to achieve this????can any one guide me..

@Override
    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);

        switch(reqCode){
           case (PICK_CONTACT):
             if (resultCode == Activity.RESULT_OK){
                 Uri contactData = data.getData();
                 Cursor c = managedQuery(contactData, null, null, null, null);

                 if (c.moveToFirst()){
                     // other data is available for the Contact.  I have decided
                     //    to only get the name of the Contact.
                     String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_TYPE));
                     Toast.makeText(getApplicationCont开发者_如何学Goext(), name, Toast.LENGTH_SHORT).show();
                 }
             }
        }

Thanks in Advance--


try this code,

   @Override
   public void onActivityResult(int reqCode, int resultCode, Intent
  data){
    super.onActivityResult(reqCode, resultCode, data);

    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK)
         {
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
          if (c.moveToFirst()) {
          String id =   
            c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

          String hasPhone =
          c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

          if (hasPhone.equalsIgnoreCase("1")) {
         Cursor phones = getContentResolver().query( 
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                      null, null);
            phones.moveToFirst();
            String cNumber = phones.getString(phones.getColumnIndex("data1"));
          }
            }
         }
    }


I know that this is an old question, but I think the below resource from Android is extremely helpful on this matter. The "bonus" section is the exact code that raj was looking for. I think this link should be helpful to anyone who sees this question in the future, especially if you don't understand CapDroid's snippet.

http://developer.android.com/training/basics/intents/result.html


This will help you:

public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);

    try {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                txt.append(name + " : " + no + "\n");

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;
            //                      populateContacts();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Log.e("IllegalArgumentException::", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Error :: ", e.toString());
    }
}
0

精彩评论

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