0

I have an app that has to show my phone contact list. The user has to select one phone number and I have to use this phone number programmatically on my app. How can I do it?

Code examples will be great.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

2 Answers2

3

Just wire up a button to the onBrowseForNumbersButtonClicked() method... drop your code in underneath the formattedPhoneNumber line... and you're good to go.

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;

public class TestActivity extends Activity {

    private static final int REQUEST_CONTACT_NUMBER = 123456789;

    /** Pops the "select phone number" window */
    public void onBrowseForNumbersButtonClicked(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, REQUEST_CONTACT_NUMBER);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(data != null && requestCode == REQUEST_CONTACT_NUMBER) {  
                Uri uriOfPhoneNumberRecord = data.getData();
                String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID + "=?", new String[]{idOfPhoneRecord}, null);
                if(cursor != null) {
                        if(cursor.getCount() > 0) {
                            cursor.moveToFirst();
                            String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
                            Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
                        }
                        cursor.close();
                }
            }
            else {
                Log.w("TestActivity", "WARNING: Corrupted request response");
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            Log.i("TestActivity", "Popup canceled by user."); 
        }
        else {
            Log.w("TestActivity", "WARNING: Unknown resultCode");
        }
    }
}
Skylar Sutton
  • 4,632
  • 3
  • 27
  • 39
  • when i try to do the first example, i got error doing the IMPORTS.... The import android.provider.ContactsContract cannot be resolved – NullPointerException Dec 03 '10 at 00:05
  • The JavaDoc is your best friend: http://developer.android.com/reference/android/provider/ContactsContract.html "Since: API Level 5" = You need Android 2.0 or better... If you absolutely have to use Android 1.6, just change the references to the old 1.6 libraries – Skylar Sutton Dec 07 '10 at 19:10
1

You need to combine a contact picker, with retrival of phone number from a given contact.

Check this Essentials to pick a contact and how to read contact data

Community
  • 1
  • 1
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • This is the correct answer and deserves the credit... I didn't see it when I posted my response. – Skylar Sutton Dec 01 '10 at 21:13
  • no this is not the correct answer, it doesn't works, maybe it's deprecated or something, when i try to do the first example, i got error doing the IMPORTS.... The import android.provider.ContactsContract cannot be resolved – NullPointerException Dec 03 '10 at 00:01
  • The above linked example codes works on 2.0+. Which SDK are you using? – Pentium10 Dec 03 '10 at 06:23