Open device contacts list at button click event

前端 未结 4 1864
名媛妹妹
名媛妹妹 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...
        }
    

提交回复
热议问题