Open device contacts list at button click event

2019-02-18 19:24发布

问题:

How can i open Android device contacts list at button click event.

回答1:

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);

    }

}


回答2:

Declare Some variables. Create a method & 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");
    }
}


回答3:

You can use this source code as a reference:

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);
            }
        });

    }
}


回答4:

if u want to pick contact from your device then 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...
    }