Open device contacts list at button click event

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

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

4条回答
  •  暗喜
    暗喜 (楼主)
    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");
        }
    }
    

提交回复
热议问题