How to call Android contacts list AND Select one phone number from its details screen?

前端 未结 6 1209
余生分开走
余生分开走 2020-12-01 03:06

I have read the already posted solutions, but they dont tell how do I use system\'s contact details screen to select any ONE number to use? I am developing an sms sending an

相关标签:
6条回答
  • 2020-12-01 03:24

    this code just work fine with me

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    
        Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
        super.startActivityForResult(i, 1001);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    
        switch (requestCode) {
        case 1001:
    
            if (resultCode == Activity.RESULT_OK) {
    
                Cursor s = getContentResolver().query(Phone.CONTENT_URI, null,
                        null, null, null);
    
            if (s.moveToFirst()) {
                    String phoneNum = s.getString(s.getColumnIndex(Phone.NUMBER));
                    Toast.makeText(getBaseContext(), phoneNum, Toast.LENGTH_LONG).show();
                                }
    
            }
    
            break;
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 03:32

    Try this code,i am sure it will work

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, PICK_CONTACT);
    
    0 讨论(0)
  • 2020-12-01 03:32

    TRY THIS

    setContentView(R.layout.main);
    
       contactNumber = (TextView)findViewById(R.id.contactnumber);
    
       Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
       buttonPickContact.setOnClickListener(new Button.OnClickListener(){
    
       @Override
        public void onClick(View arg0) {
       // TODO Auto-generated method stub
    
    
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
       intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
       startActivityForResult(intent, 1);             
    
    
        }});
       }
    
       @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    
       if(requestCode == RQS_PICK_CONTACT){
       if(resultCode == RESULT_OK){
        Uri contactData = data.getData();
        Cursor cursor =  managedQuery(contactData, null, null, null, null);
        cursor.moveToFirst();
    
          String number =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
          //contactName.setText(name);
          contactNumber.setText(number);
          //contactEmail.setText(email);
         }
         }
         }
         }
    

    EDIT XML

    <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="vertical" >
    
          <Button
            android:id="@+id/pickcontact"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Pick Contact" />
    
          <TextView
           android:id="@+id/contactnumber"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content" />
    
     </LinearLayout>
    
    0 讨论(0)
  • 2020-12-01 03:35

    I've got few codes about this on SO, but in the end I implement code from android guide website https://developer.android.com/guide/components/intents-common#PickContactDat . I tried the code and I dont need implement any runtime permission request like READ_CONTACTS.

    static final int REQUEST_SELECT_PHONE_NUMBER = 1;
    
    public void selectContact() {
        // Start an activity for the user to pick a phone number from contacts
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
            // Get the URI and query the content provider for the phone number
            Uri contactUri = data.getData();
            String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, projection,
                    null, null, null);
            // If the cursor returned is valid, get the phone number
            if (cursor != null && cursor.moveToFirst()) {
                int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(numberIndex);
                // Do something with the phone number
                //...
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:41

    phew, it took me some time, but i think i have the answer you need (even if it's too late already, but i'll still post it as a reference for others).

    in the application i'm currently developing the user may enter a phone number into an EditText or click on a button and select a person from the phones address book. if the person has more than one phone number, there's a drop down list where he can select exactly one of them.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_picker);
    
        // this opens the activity. note the  Intent.ACTION_GET_CONTENT
        // and the intent.setType
        ((Button)findViewById(R.id.pick_person)).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);                
            }
        });
    }
    

    now, as soon as the user selects a contact (and probably chooses one of several phone numbers), you can retrieve the data the normal way:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Uri uri = data.getData();
    
            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver().query(uri, new String[]{ 
                                ContactsContract.CommonDataKinds.Phone.NUMBER,  
                                ContactsContract.CommonDataKinds.Phone.TYPE },
                            null, null, null);
    
                    if (c != null && c.moveToFirst()) {
                        String number = c.getString(0);
                        int type = c.getInt(1);
                        showSelectedNumber(type, number);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }
    
    public void showSelectedNumber(int type, String number) {
        Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      
    }
    

    here's the documentation for CommonDataKinds.Phone for on dev.android.

    the int "type" tells you the type of number: mobile (2), home (1), work (3), and so on.

    note: after the user selects the contact, he gets a spinner of numbers with no indication of the numbers type. that's not really user friendly: if a contact has 5 assigned numbers ... uh, which one of these is the fax number again?

    another note: above example needs the sdk > 5 (Android 2.0+), so no 1.6 (=sdk 4). 1.6 has a different api, and if you want to support both versions, read the article about the contacts API on dev.android.

    good luck.

    disclaimer: i copied most of my code out of the PickContact.java example

    0 讨论(0)
  • 2020-12-01 03:43

    There is a problem in the accepted answer. If the number selected contains spaces within it
    i.e. 85 29 948789 then it will show only 85 (until first space).

    so use the below code to rectify this problem :)

    Intent intent1 = new Intent(Intent.ACTION_PICK);
    intent1.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent1, 1);
    

    and in onActivityResult

        Uri contactUri = data.getData();
    
        String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
    
    
        Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
        cursor.moveToFirst();
    
    
        int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        String number = cursor.getString(numberColumn);
        String name = cursor.getString(nameColumn);
    
    0 讨论(0)
提交回复
热议问题