2014-07-06 100 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