Android 如何访问电话簿联系人?

Android 如何访问电话簿联系人?,android,android-contacts,android-cursor,Android,Android Contacts,Android Cursor,我正在使用编辑文本和按钮。按一个按钮,电话簿打开,然后用户将从中选择联系人,所选电话号码将显示在编辑文本上。 我学习了很多教程,但是它们所展示的方法已经被贬低了 我已声明此权限:读取清单中的联系人。Digvesh Patel提供的答案正确。他使用了联系人的“类型”,返回号码。因此,我使用了他的代码,并在应用程序中进行了一些更改。这可能对某人有帮助 @Override protected void onCreate(Bundle savedInstanceState) {

我正在使用编辑文本和按钮。按一个按钮,电话簿打开,然后用户将从中选择联系人,所选电话号码将显示在编辑文本上。 我学习了很多教程,但是它们所展示的方法已经被贬低了


我已声明此权限:读取清单中的联系人。Digvesh Patel提供的答案正确。他使用了联系人的“类型”,返回号码。因此,我使用了他的代码,并在应用程序中进行了一些更改。这可能对某人有帮助

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_picker);

        // this opens the activity. note the  Intent.ACTION_GET_CONTENT
        // and the intent.setType
        ((Button)findViewById(R.id.pick_person)).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);                
            }
        });
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String number = c.getString(0);
                    int type = c.getInt(1);
                    showSelectedNumber(type, number);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      
}
public int REQUESTCODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button select = (Button) findViewById(R.id.select);
    select.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, REQUESTCODE);

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();
        Log.i("data", uri.toString());
        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String name = c.getString(0);
                    String number = c.getString(1);
                    int type = c.getInt(2);

                    showSelectedNumber(name, number,type);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(String name,  String number, int type){
    TextView usernumber = (TextView) findViewById(R.id.textView1);
    String typelabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), type, "");
    usernumber.setText(name+": "+number+" "+typelabel);

}

我必须声明除READ_Contacts One.之外的任何其他权限吗。。但它不会在编辑文本上显示所选联系人,只显示祝酒词