Open the device contact list by pressing the button

How can I open the contact list of Android devices when a button is clicked.

+3
source share
4 answers

Try this code ..

yourButton.setOnClickListener(new YouButtonEvent()); class YouButtonEventimplements OnClickListener{ @Override public void onClick(View v) { Intent it= new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(it, PICK_CONTACT); } } 
+9
source

Declare some variables. Create a method and handle the events.

 private static final int CONTACT_PICKER_RESULT = 1001; private static final String DEBUG_TAG = "Contact List"; private static final int RESULT_OK = -1; // a method to open your contact list private void openContactList() { Intent it = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(it, CONTACT_PICKER_RESULT); } // handle after selecting a contact from the list @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case CONTACT_PICKER_RESULT: // handle contact results Log.w(DEBUG_TAG, "Warning: activity result is ok!"); break; } } else { // gracefully handle failure Log.w(DEBUG_TAG, "Warning: activity result not ok"); } } 
+4
source

You can use this source code as a link:

 import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Test1Activity extends Activity { private static final int PICK_CONTACT_REQUEST = 1; private static final int PICK_CONTACT = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button pickContact = (Button) findViewById(R.id.button1); pickContact.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT); i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); startActivity(i); } }); } } 
+3
source

If you want to select a contact from your device, use this code.

 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openContect(); dialog.dismiss(); } 

and openContact() is:

  private void openContect() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_CONTACT); } } 

and in your onActivityResult() use this:

 if (requestCode==REQUEST_SELECT_CONTACT && resultCode == RESULT_OK && null != data){ Uri contactUri = data.getData(); //do what you want... } 
+1
source

Source: https://habr.com/ru/post/912124/


All Articles