1

I'm making an Android app, allow me to call the contacts list function, pick a contact, then return to my app with the contact's number, but I can only figure out how to get the Name.

I had it working by following a tutorial at one point, but the class People is depreciated so I started over.

Here is my code so far:

public class Contacts extends Activity {

    int PICK_CONTACT;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText number=(EditText)findViewById(R.id.number);
        Button contactButton = (Button) findViewById(R.id.contacts);
        contactButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                  Intent intent = new Intent(Intent.ACTION_PICK);
                  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                  startActivityForResult(intent, PICK_CONTACT);
            }
        });     
        Button dial=(Button)findViewById(R.id.dial);
        dial.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
        String toDial="tel:"+number.getText().toString();
        startActivity(new Intent(Intent.ACTION_DIAL,
        Uri.parse(toDial)));
        }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {

      if (requestCode == PICK_CONTACT)
      {         
          Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
          cursor.moveToNext();
         // String contactId =     cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

          Toast.makeText(this, "did it work?  =  "+name, Toast.LENGTH_LONG).show(); 
          finish(); 
          }
    }}

Thanks!

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Colby
  • 296
  • 1
  • 6
  • 22
  • please can have l a look on https://stackoverflow.com/questions/6155612/getting-number-from-contacts-picker , it can help you. – Prags Nov 08 '17 at 11:30

0 Answers0