2010-12-18 70 views
13

我想我的應用程序與聯繫人管理器集成:整合我的應用程序聯繫

更確切地說:

當我在我的手機上運行聯繫的應用程序,然後我點擊任何頭像,彈出(快速聯繫人徽章)窗口出現與一些應用程序來選擇(聯繫人,郵件等)我想添加我的應用程序在那個地方。

這可能嗎?

我希望能夠清楚。

在此先感謝。

+0

你是如何解決它的?我也想這樣做。 – chrisonline 2011-05-19 13:10:39

+0

到目前爲止沒有運氣,是我的TODO列表中的一個未決任務,就像找到一些解決方案,我會在這裏發佈它。請做同樣的:)。 – vsm 2011-05-19 16:32:07

回答

14

嘿傢伙終於我解決了這個添加一個自定義字段到ContactProvider,然後QuickContactBadge將鏈接它給你。

我的代碼,用於添加,刪除特定條目,刪除我添加的所有條目。

private static final IM_LABEL = "Test protocol"; 
private static final LOG_TAG = "Log" 
    /** 
* This method add my account under IM field at default Contact 
* application 
* 
* Labeled with my custom protocol. 
* 
* @param contentResolver 
*   content resolver 
* @param uid 
*   User id from android 
* @param account 
*   account name 
*/ 
public static void updateIMContactField(ContentResolver contentResolver, 
     String uid, String account) { 

    ContentValues contentValues = new ContentValues(); 

    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, 
      Integer.parseInt(uid)); 
    contentValues.put(ContactsContract.Data.MIMETYPE, 
      ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE); 
    contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE, 
      ContactsContract.CommonDataKinds.Im.TYPE_CUSTOM); 
    contentValues.put(ContactsContract.CommonDataKinds.Im.LABEL, IM_LABEL); 
    contentValues.put(ContactsContract.CommonDataKinds.Im.PROTOCOL, 
      ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM); 
    contentValues.put(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, 
      IM_LABEL); 

    contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, account); 

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    ops.add(ContentProviderOperation 
      .newInsert(ContactsContract.Data.CONTENT_URI) 
      .withValues(contentValues).build()); 

    try { 
     contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     Log.d(LOG_TAG, "Can't update Contact's IM field."); 
    } 
} 

/** 
* This method remove IM entry at default Contact application. 
* 
* @param contentResolver 
*   content resolver 
* @param uid 
*   User id from android 
* @param account 
*   account name 
*/ 
public static void removeIMContactField(ContentResolver contentResolver, 
     String uid, String account) { 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    ops.add(ContentProviderOperation 
      .newDelete(Data.CONTENT_URI) 
      .withSelection(
        ContactsContract.Data.RAW_CONTACT_ID + "=? and " 
          + ContactsContract.Data.MIMETYPE + "=? and " 
          + ContactsContract.CommonDataKinds.Im.DATA 
          + " = ?", 
        new String[] { 
          String.valueOf(uid), 
          ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE, 
          account }).build()); 

    try { 
     contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     Log.d(LOG_TAG, "Can't delete Contact's IM field."); 
    } 
} 

/** 
* This method remove IM all entries at default Contact application 
* 
* @param contentResolver 
*   content resolver 
*/ 
public static void deleteAllIMContactField(ContentResolver contentResolver) { 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    ops.add(ContentProviderOperation 
      .newDelete(Data.CONTENT_URI) 
      .withSelection(
        ContactsContract.Data.MIMETYPE 
          + "= ? and " 
          + ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL 
          + "= ?", 
        new String[] { 
          ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE, 
          IM_LABEL }).build()); 

    try { 
     contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     Log.d(LOG_TAG, 
       "An exception occurred when deleting all IM field of Contact."); 
    } 
} 

希望有人找到這個有用的。

+0

你能否澄清一下:「嗨,我終於解決了這個問題,在ContactProvider中添加一個自定義字段,然後QuickContactBadge會爲你鏈接。」 – Chrispix 2011-11-09 17:53:32

+1

如果您將自定義字段添加到ContactProvider,則您的應用將被列入QuickContactBadge。檢查手機上的默認聯繫人應用程序,然後「點擊」一個聯繫人,彈出窗口將顯示(徽章)與一些可用的應用程序。我的意圖是將我的應用添加到該徽章中,並且我發現您可以添加自己的字段並在該字段中放置一些信息,然後android會將您的應用與該特定聯繫人關聯,因爲我的應用擁有該自定義字段。 – vsm 2011-11-11 05:57:16

+0

你能告訴我什麼是帳戶名?它只是簡單的聯繫人姓名嗎?當我嘗試這個時,它給我例外applyBatch方法。請幫幫我。 – Rahil2952 2013-10-11 06:49:12

相關問題