Open device contacts list at button click event

前端 未结 4 1818
名媛妹妹
名媛妹妹 2021-01-14 12:34

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

相关标签:
4条回答
  • 2021-01-14 12: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...
        }
    
    0 讨论(0)
  • 2021-01-14 12:46

    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);
                }
            });
    
        }
    }
    
    0 讨论(0)
  • 2021-01-14 12:53

    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");
        }
    }
    
    0 讨论(0)
  • 2021-01-14 13:06

    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);
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题