开发者

How can I show my contact phone numbers and get one of the phone numbers for use on my app?

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 开发者_如何学JAVAon my app. How can I do it?

Code examples will be great.


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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜