0

This code can open a built-in contacts directory on a mobile device. However, when I select a contact, the app reports "done", but doesn't retrieve the contact number -- the user can't send a message.

How do I retrieve the contact number? List View is not a solution: I need to return just the one number, not extra information.

Here's my code:

public EditText no;
public EditText msg;
public Button cancel;
public Button snd;
public String Number,name,Message;
public int run=0;
public static final int PICK_CONTACT=1;



public void callContacts(View v)
{
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent,PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode,int resultCode,Intent data)
{
    super.onActivityResult(reqCode,resultCode,data);
    if(reqCode==PICK_CONTACT)
    {
        if(resultCode== ActionBarActivity.RESULT_OK)
        {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData,null,null,null,null);
            if(c.moveToFirst())
            {
                name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_URI.toString()));
                Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

and here is the onCreate method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);

    no = (EditText)findViewById(R.id.num);
    msg = (EditText)findViewById(R.id.sms);
    cancel = (Button)findViewById(R.id.cancel);
    snd = (Button)findViewById(R.id.snd);
    no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            msg.setText("");
            no.setText("");
            callContacts(null);
        }
    });
    snd.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Number = no.toString();
            Message = msg.getText().toString();
            SmsManager manager = SmsManager.getDefault();
            ArrayList<String>long_sms = manager.divideMessage(Message);
            manager.sendMultipartTextMessage(Number,null,long_sms,null,null);
            Toast.makeText(getApplicationContext(),"Sent Successfully",Toast.LENGTH_SHORT).show();
        }
    });
}
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Tightened wording; corrected grammar. – Prune Sep 20 '16 at 16:34
  • Possible duplicate of [Pick a Number and Name From Contacts List in android app](http://stackoverflow.com/questions/9496350/pick-a-number-and-name-from-contacts-list-in-android-app) – Bryan Sep 20 '16 at 16:43

1 Answers1

0

Here is the code just for selecting one phone number from contact list. i found this code simplest. let me know if there is any problem.

start activity with pick intent on phone data type:

findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
      }
 });

Now set onAcitivityResult();

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        switch (requestCode){
            case PICK_CONTACT_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri contactUri = intent.getData();
                    Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
                    if (nameCursor.moveToFirst()){
                        String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        ((EditText)findViewById(R.id.person_name)).setText(name);
                        ((EditText)findViewById(R.id.enter_mobile)).setText(number);
                        nameCursor.close();
                    }
                }
                break;
        }
    }
Nishant Bhakta
  • 2,897
  • 3
  • 21
  • 24