2014-07-06 67 views
1

我知道获得的电话号码如何从电话簿只选择电话号码意图

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

意图,但我不知道怎么弄的电话号码,而无需请求接触onActivityResult读取权限()。

谢谢。

回答

0

尝试用

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
startActivityForResult(intent, 1); 
0
@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)之前,在 联系供应商进行查询(如上面所示)要求您应用程序 声明READ_CONTACTS权限(请参阅安全性和权限)。 但是,从Android 2.3开始,联系人/人应用授予 您的应用临时权限,可在联系供应商 返回结果时进行读取。临时权限仅适用于 请求的特定联系人,因此除非您声明 READ_CONTACTS权限,否则您无法查询除意图的Uri指定的联系人以外的其他联系人 。

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