开发者

Sending SMS in Android,

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.


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:
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜