«

使用Intent获取系统联系人

时间:2024-3-2 18:04     作者:韩俊     分类: Android


跳转

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                intent.setType(Phone.CONTENT_TYPE);
                this.startActivityForResult(intent, 10001);

获取联系人
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10001) {
            if(resultCode == RESULT_OK){
                // Get the URI that points to the selected contact
                Uri contactUri = data.getData();
                // We only need the NUMBER column, because there will be only one row in the result
                String[] projection = {Phone.NUMBER,Phone.DISPLAY_NAME};

                // Perform the query on the contact to get the NUMBER column
                // We don't need a selection or sort order (there's only one result for the given URI)
                // CAUTION: The query() method should be called from a separate thread to avoid blocking
                // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                // Consider using CursorLoader to perform the query.
                Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
                cursor.moveToFirst();

                String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));

                cursor.close();
            }
        }
    }

标签: android

热门推荐