【问题标题】:How to pick just the phone number from Phone book with Intent如何使用 Intent 从电话簿中仅选择电话号码
【发布时间】:2014-07-06 21:17:45
【问题描述】:

我知道获取电话号码的意图

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

但我不知道如何在不请求 onActivityResult() 中的联系人读取权限的情况下获取电话号码。

谢谢。

【问题讨论】:

    标签: android android-intent android-contacts


    【解决方案1】:

    尝试用

    替换您的代码
    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, 1);
    

    【讨论】:

      【解决方案2】:
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // Check which request it is that we're responding to
          if (requestCode == GET_CONTACT_NUMBER) {
              // Make sure the request was successful
              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};
      
                  // 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();
      
                  // Retrieve the phone number from the NUMBER column
                  int column = cursor.getColumnIndex(Phone.NUMBER);
                  String number = cursor.getString(column);
      
                  // Do something with the phone number...
              }
          }
      }
      

      注意:在 Android 2.3(API 级别 9)之前,对 Contacts Provider(如上图所示)要求您的应用 声明 READ_CONTACTS 权限(请参阅安全和权限)。 但是,从 Android 2.3 开始,Contacts/People 应用程序授予 您的应用程序具有从联系人提供程序读取的临时权限 当它返回结果时。临时许可仅适用于 请求的特定联系人,因此您无法查询其他联系人 比意图的 Uri 指定的那个,除非你声明 READ_CONTACTS 权限。

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

      【讨论】:

        猜你喜欢
        • 2014-08-24
        • 2017-03-05
        • 1970-01-01
        • 2013-03-07
        • 2018-01-08
        • 2011-10-25
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多