android – 没有权限读取联系人?

我想通过Contacts Picker阅读联系人,如下所示:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contact,CONTACT_PICK_CODE);

如果我得到结果,那么intent.getData()包含一个用于查找联系人的URI,但我需要权限READ_CONTACTS才能读取它.

我认为有可能在没有此许可的情况下接收联系人,类似于CALL权限:如果我想直接拨打电话,我需要它,但没有它,我可以向手机应用程序发送一个号码,用户必须点击通话按钮. READ_CONTACTS是否有类似的功能我不知道?

解决方法

您可以在没有权限的情况下检索联系信息,就像您在问题中所说的那样.

在简历中,您创建了一个选择联系人的意图,这为您提供了一个URI(并且在时间上也授予您阅读权限),然后您使用URI查询以使用Contact Provider API检索数据.

您可以在Intents guide阅读更多相关信息.

例如(来自指南):

static final int REQUEST_SELECT_PHONE_NUMBER = 1;

public void selectContact() {
    // Start an activity for the user to pick a phone number from contacts
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent,REQUEST_SELECT_PHONE_NUMBER);
    }
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri,projection,null,null);
        // If the cursor returned is valid,get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...