how do i open contacts when i click a button defined in main.xml

前端 未结 3 1916
一向
一向 2020-12-15 12:30

I am developing a gps tracking app in android. I am done with displaying the map n stuff. Now I want to make a button on top which when clicked would display the contacts, T

相关标签:
3条回答
  • 2020-12-15 13:00

    try this code

    Intent intent = new Intent(Intent.ACTION_DEFAULT, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, 1);
    

    Use ACTION_DEFAULT instead of ACTION_PICK.

    Good Luck.

    0 讨论(0)
  • 2020-12-15 13:02

    You can set an Event on Button click by setting an OnClickListener on the Button with the following code, and use Intent to call ContactPicker activity:

    button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent intent= new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);
    
            startActivityForResult(intent, PICK_CONTACT);
    
                }
            });
    

    and in onActivityResult() process the contact uri to load details of contact.

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);
    
      switch (reqCode) {
        case (PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c =  managedQuery(contactData, null, null, null, null);
            if (c.moveToFirst()) {
              String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
              // TODO Fetch other Contact details as you want to use
    
            }
          }
          break;
      }
    }
    
    0 讨论(0)
  • 2020-12-15 13:06

    You should use startActivityForResult

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
    startActivityForResult(intent, 1); 
    

    See "get contact info from android contact picker" for more information.

    0 讨论(0)
提交回复
热议问题