tags:

views:

40

answers:

1

I have already implemented sending message to multiple users. Now here is what i want to do further

  1. I have button on My main activity on button click Android's Default contact book should open
  2. When i click on the particular contact from phonebook, then particular phone number from that selected contact should occur in editbox in my main activity.

I have called intent on click event of button like this

addcontact.setOnClickListener(new View.OnClickListener() {

    public void onClick(View V) {
        Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);             
    }
});

Now i m stuck in how to retrieve phone number from OnActivity results.

A: 

This should work though I haven't tested it:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Cursor cursor = null;
    String number = "";
    Uri result = data.getData();

    // get the contact id from the Uri
    String id = result.getLastPathSegment();

    Cursor phones = getContentResolver().query(Phone.CONTENT_URI, null,
    Phone.CONTACT_ID + " = " + id, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
        case Phone.TYPE_HOME:
            // do something with the Home number here...
            break;
        case Phone.TYPE_MOBILE:
            // do something with the Mobile number here...
            break;
        case Phone.TYPE_WORK:
            // do something with the Work number here...
            break;
        }
    }
    phones.close();
}

You need to use onAcitivtyResult and a ContentResolver to get the numbers. You may also want to check that the returned result is actually from your ContactPickerIntent and not a different activity via:

switch (requestCode) 
        case CONTACT_PICKER_RESULT:
BeRecursive
I have got the solution...Thanks for the effort...I would like to add that if we are using like this then it will work on version 2.0,2.2 But it will not work on 1.6..Anyway thanks for your effort
Rakesh Gondaliya