Open device contacts list at button click event

2019-02-18 19:41发布

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

4条回答
放我归山
2楼-- · 2019-02-18 19:44

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...
    }
查看更多
Summer. ? 凉城
3楼-- · 2019-02-18 19:50

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

    }

}
查看更多
仙女界的扛把子
4楼-- · 2019-02-18 19:56

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");
    }
}
查看更多
Emotional °昔
5楼-- · 2019-02-18 19:59

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

    }
}
查看更多
登录 后发表回答