2012-11-29 29 views
1

我正在製作一個應用程序,該應用程序會詢問現有聯繫人,然後爲該聯繫人保留某種標識符,並最終能夠在稍後查找該聯繫人的信息(具體來說,名稱和圖片)。要求用戶選擇一個聯繫人,然後能夠查找該聯繫人的信息?

我有選擇器意圖:

Intent contactIntent = new Intent(Intent.ACTION_PICK, 
        ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(contactIntent, 5); 

和結果:

public void onActivityResult(int reqCode, int resultCode, Intent data) { 
    super.onActivityResult(reqCode, resultCode, data); 

    switch (reqCode) { 
    case (pickCode): 
     if (resultCode == RESULT_OK) { 
      Uri contactData = data.getData(); 

但是我在這裏丟失。我如何「標記」這個聯繫人,以便稍後可以找到它?

一旦我這樣做了,我該如何獲取聯繫人圖片?儘管我可以弄清楚這一點,但如果有一些簡單的方法可以融入第一部分,那會很好。

回答

2

可以在SharedPreferences存儲此聯繫人的URI以供日後使用或用於其他組件之間共享爲:

創建SharedPreferences爲:

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", 
              MODE_WORLD_READABLE); 
SharedPreferences.Editor prefsEditor = myPrefs.edit(); 
prefsEditor.putString("contact_uri", contactData.toString()); 
prefsEditor.commit(); 

,並從SharedPreferences檢索URI後:

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", 
               MODE_WORLD_READABLE); 
String contact_uri = myPrefs.getString("contact_uri", "not found"); 
Uri contactData = Uri.parse(contact_uri); 
相關問題