2016-01-20 116 views
1

我正在編寫一個應用程序,我需要向用戶顯示一個聯繫人。我有電話號碼,我可以查詢以獲取聯繫詳情。像這樣:如何顯示設備的主要聯繫人查看器顯示與電話號碼的一個聯繫人

ContentResolver cr = context.getContentResolver(); 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(phoneNumber)); 
    Cursor cursor = cr.query(uri, 
      new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    String contactName = phoneNumber; 
    if (cursor.moveToFirst()) { 
     contactName = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
    } 

但是我需要顯示設備的主要聯繫人查看器,我不知道如何?

像這樣:

enter image description here

+0

這是所有記錄在這裏:https://developer.android.com/guide/components/intents-common.html在章節「聯繫人/人的應用程序」 –

+0

@MarcinOrlowski感謝您的回覆。但不幸的是,我無法訪問您的鏈接,也沒有谷歌文件也在我的地區! – Beppe

+0

令人印象深刻。我發佈了相關報價的回覆。 –

回答

1

這在Android官方文檔的所有文檔,Common Intents,章 「聯繫人/聯繫人應用」。

編輯

但遺憾的是我不能訪問到你的鏈接,也不也是谷歌文件在我的領域!

我這裏還有來自鏈接文檔報價:

查看聯繫人

要爲已知的接觸顯示詳細信息,請使用ACTION_VIEW行動,並指定與內容的接觸:如URI意圖數據。

主要有兩種方式,首先檢索聯繫人的URI:

  • 使用由ACTION_PICK返回的接觸URI,在上一節中顯示(此方法不需要任何應用程序的權限)。
  • 直接訪問所有聯繫人的列表,如檢索聯繫人列表中所述(此方法需要READ_CONTACTS權限)。

實施例:

public void viewContact(Uri contactUri) { 
    Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); 
    if (intent.resolveActivity(getPackageManager()) != null) { 
     startActivity(intent); 
    } 
} 

編輯現有的接觸

要編輯已知的接觸,使用ACTION_EDIT動作,指定與內容的接觸:URI爲意圖數據,以及在ContactsContract.Intents.Insert中包含由常量指定的附加信息中的任何已知聯繫人信息。

主要有兩種方式,首先檢索聯繫人URI:

  • 使用由ACTION_PICK返回的接觸URI,在上一節中顯示(此方法不需要任何應用程序的權限)。

  • 直接訪問所有聯繫人的列表,如檢索聯繫人列表(此方法需要READ_CONTACTS權限)中所述。

注意:Extras - 一個或多個在ContactsContract.Intents.Insert中定義的附加項,以便您可以填充聯繫人詳細信息的字段。

public void editContact(Uri contactUri, String email) { 
    Intent intent = new Intent(Intent.ACTION_EDIT); 
    intent.setData(contactUri); 
    intent.putExtra(Intents.Insert.EMAIL, email); 
    if (intent.resolveActivity(getPackageManager()) != null) { 
     startActivity(intent); 
    } 
} 

有關如何編輯聯繫人的更多信息,請閱讀Modifying Contacts Using Intents

+0

謝謝@MarcinOrlowski。你是夜晚的燈光。 – Beppe

相關問題