有些手机需要许可READ_CONTACTS从联系人选择器阅读 [英] Some phones need permission READ_CONTACTS to read from contact picker

查看:191
本文介绍了有些手机需要许可READ_CONTACTS从联系人选择器阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HTC ONE M7 (GPE 4.2.2)和 HTC EVO 3D (4.0.3)的HTC Sense 3.6

HTC ONE 不需要

 <使用许可权的android:NAME =android.permission.READ_CONTACTS/>

HTC EVO 3D 但是,下面的code抛出异常:

 公共静态字符串getPhoneNumberFromIntent(上下文的背景下,意图数据)抛出SecurityException异常{
    字符串contactNumber = NULL;
    最后乌里contactUri = data.getData();
    如果(contactUri!= NULL){
        光标C = NULL;
        尝试{
            从联系人提供商//读取联系人号码
            的String [] =投影新的String [] {} ContactsContract.CommonDataKinds.Phone.NUMBER;
            C = context.getContentResolver()查询(contactUri,投影,NULL,NULL,NULL);
            如果(C =空&放大器;!&放大器; c.moveToFirst()){
                INT maxNumberLength = context.getResources()getInteger(R.integer.max_phone_number_cze)。
                contactNumber = cutOnlyLastPhoneNumberDigits(c.getString(0),maxNumberLength);
            }
        } {最后
            如果(C!= NULL){
                c.close();
            }
        }
    }
    返回contactNumber;
}

-

  java.lang.SecurityException异常:权限拒绝:阅读com.android.providers.contacts.HtcContactsProvider2
URI内容:从PID = 14938,UID //com.android.contacts/data/2158 = 10125要求android.permission.READ_CONTACTS

我已阅读,当用户通过手的接触appliaction被授予所需的权限。但是在某些手机上不起作用(HTC EVO 3D)。

这是为什么happending?有一种解决方法,例如在运行时,要求此权限的能力吗?


解决方案

  

HTC ONE并不需要:<使用许可权的android:NAME =android.permission.READ_CONTACTS/> 上HTC EVO 3D然而,随着code抛出一个异常


如果在乌里你正在从 ACTION_PICK 即将或 ACTION_GET_CONTENT ,你是否拥有该联系人的临时读取权限将通过接触采摘的应用程序有所不同。


  

我已阅读,当用户通过手的接触appliaction被授予所需的权限。但是在某些手机上不起作用(HTC EVO 3D)。


有没有要求所有接触采摘的应用程序授予您的联系人临时读取访问。其实,我不清楚,如果第三方联系人管理器必须授予您的联系人临时读访问的能力。


  

是否有这样一种解决方法是要求在运行此权限的能力吗?


您不能要求在运行时的权限。

您的选择是:


  1. 在清单总是询问 READ_CONTACTS 。这将确保你可以做你想做的,在请求另一个许可,一个未来的用户可能不喜欢的费用。


  2. 办理的 SecurityException异常,只是离不开你试图查询的数据(),如果该数据不是必须的。


  3. 编写与可以作为一个插件为您的应用程序,安全地取回代表你的主要应用程序的联系人数据的 READ_CONTACTS 权限的单独的应用程序。然后,您可以将用户路由谁获得 SecurityException异常来安装您的联系人插件。这是棘手,而不会引入安全漏洞来写,所以我会鼓励你使用任何其他选项。


I have a HTC ONE M7 (GPE 4.2.2) and HTC EVO 3D (4.0.3) HTC Sense 3.6

HTC ONE does not need:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

on HTC EVO 3D however, following code throws an exception:

 public static String getPhoneNumberFromIntent(Context context, Intent data) throws SecurityException {
    String contactNumber = null;
    final Uri contactUri = data.getData();
    if (contactUri != null) {
        Cursor c = null;
        try {
            // Read contact number from contacts provider
            String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
            c = context.getContentResolver().query(contactUri, projection, null, null, null);
            if (c != null && c.moveToFirst()) {
                int maxNumberLength = context.getResources().getInteger(R.integer.max_phone_number_cze);
                contactNumber = cutOnlyLastPhoneNumberDigits(c.getString(0), maxNumberLength);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
    return contactNumber;
}

-

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2
uri content://com.android.contacts/data/2158 from pid=14938, uid=10125 requires android.permission.READ_CONTACTS

I have read that appliaction is granted required permissions when user selects contact by hand. However on some phones this does not work (HTC EVO 3D).

Why is this happending? Is there a workaround such is ability to ask for this permission at runtime ?

解决方案

HTC ONE does not need: <uses-permission android:name="android.permission.READ_CONTACTS" /> on HTC EVO 3D however, following code throws an exception

If the Uri you are getting is coming from ACTION_PICK or ACTION_GET_CONTENT, whether or not you have temporary read permissions for that contact will vary by contact-picking app.

I have read that appliaction is granted required permissions when user selects contact by hand. However on some phones this does not work (HTC EVO 3D).

There is no requirement that all contact-picking apps grant you temporary read access to the contact. In fact, I am unclear if third-party contact managers would have the ability to grant you temporary read access to the contact.

Is there a workaround such is ability to ask for this permission at runtime ?

You cannot ask for a permission at runtime.

Your choices are:

  1. Always ask for READ_CONTACTS in the manifest. This ensures you can do what you want, at the cost of requesting another permission, one that prospective users might not like.

  2. Handle the SecurityException and simply do without the data that you are trying to query(), if that data is not essential.

  3. Write a separate app with the READ_CONTACTS permission that can serve as a "plugin" for your app, securely retrieving contact data on behalf of your main app. You can then route users who get the SecurityException to install your contacts plugin. This is tricky to write without introducing security flaws, so I would encourage you to use either of the other options.

这篇关于有些手机需要许可READ_CONTACTS从联系人选择器阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆