【问题标题】:Get selected phone number from contacts list without READ_CONTACTS permission在没有 READ_CONTACTS 权限的情况下从联系人列表中获取选定的电话号码
【发布时间】:2017-05-11 16:08:23
【问题描述】:

在我的应用程序中,我希望用户可以通过手动输入或从联系人列表中选择电话号码来用电话号码填写文本表单。我不明白的一件事是,如果用户自己选择联系人,为什么我应该设置READ_CONTACTS 权限。我正在使用下面列出的代码:

  1. 要启动联系人活动:

    Intent pickContactIntent = new Intent(Intent.ACTION_PICK,
                                     ContactsContract.Contacts.CONTENT_URI);
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST_CODE);
    
  2. 处理来自onActivityResultIntent数据:

    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();
        }
      }
    }
    

据我了解,getContentResolver().query() 需要READ_CONTACTS 权限才能获取电话号码。

我的问题:是否可以在没有READ_CONTACTS 的情况下以某种方式处理 onActivityResult 中的 Intent?

【问题讨论】:

    标签: android android-intent android-contacts android-permissions


    【解决方案1】:

    您实际上不需要 READ_CONTACTS。

    Note: Before Android 2.3 (API level 9), performing a query on the Contacts Provider (like the one shown above) requires that your app declare the READ_CONTACTS permission (see Security and Permissions). However, beginning with Android 2.3, the Contacts/People app grants your app a temporary permission to read from the Contacts Provider when it returns you a result. The temporary permission applies only to the specific contact requested, so you cannot query a contact other than the one specified by the intent's Uri, unless you do declare the READ_CONTACTS permission.

    来源:http://developer.android.com/training/basics/intents/result.html

    【讨论】:

    • 我已经尝试过那里列出的代码示例,但未经许可,我的应用程序因“Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/ 而崩溃” 937 from pid=16166, uid=10115 需要 android.permission.READ_CONTACTS 或 grantUriPermission()"
    • 我在 Htc 设备上遇到了同样的问题,但三星在没有 READ_CONTACTS 权限的情况下可以正常工作。
    【解决方案2】:

    不应该实际上需要 READ_CONTACTS。 @Tony Chu 是对的,文档说:

    注意:在 Android 2.3(API 级别 9)之前,对联系人提供程序(如上所示)执行查询需要您的应用声明 READ_CONTACTS 权限(请参阅安全性和权限)。但是,从 Android 2.3 开始,Contacts/People 应用程序会在您返回结果时授予您的应用程序从 Contacts Provider 读取的临时权限。临时权限仅适用于请求的特定联系人,因此您不能查询意图 Uri 指定的联系人以外的联系人,除非您确实声明了 READ_CONTACTS 权限。 (Source)

    然而,我注意到有些手机可能仍然需要它(根据我的经验,xperia 设备和@AVirvara 的 HTC 设备)。

    抱歉,您似乎别无选择。

    【讨论】:

      【解决方案3】:

      如果没有在清单文件中授予 READ_CONTACTS 权限,您将无法访问 Android 联系人数据库。

      Android 移动操作系统使用权限系统来帮助确保应用正常运行。该系统允许应用请求访问设备上可以使用电源、访问敏感数据和产生费用的功能。

      【讨论】:

        【解决方案4】:

        您应该在清单文件中写入权限,然后您只能从文件中读取联系人。

        【讨论】:

          【解决方案5】:

          无论是否在清单中放置权限,您都需要即时向用户请求该权限。

          if( getApplicationContext().checkSelfPermission( Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED ) ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, resultValue);

          只有在审核通过后,您才能查询联系人的电话号码/姓名等。

          【讨论】:

            猜你喜欢
            • 2019-03-19
            • 1970-01-01
            • 2020-12-21
            • 2017-04-13
            • 1970-01-01
            • 1970-01-01
            • 2013-05-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多