打开一个活动来编辑同步适配器中的联系人



在Android的SampleSyncAdapter中有如下一段代码:

/**
 * Adds a profile action
 *
 * @param userId the userId of the sample SyncAdapter user object
 * @return instance of ContactOperations
 */
public ContactOperations addProfileAction(long userId) {
    mValues.clear();
    if (userId != 0) {
        mValues.put(SampleSyncAdapterColumns.DATA_PID, userId);
        mValues.put(SampleSyncAdapterColumns.DATA_SUMMARY, mContext
            .getString(R.string.syncadapter_profile_action));
        mValues.put(SampleSyncAdapterColumns.DATA_DETAIL, mContext
            .getString(R.string.view_profile));
        mValues.put(Data.MIMETYPE, SampleSyncAdapterColumns.MIME_PROFILE);
        addInsertOp();
    }
    return this;
}

我为我的活动添加了这个过滤器

    <intent-filter>
        <action android:name="@string/syncadapter_profile_action" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="vnd.android.cursor.item/vnd.myapp.profile"
            android:host="contacts" />
     </intent-filter>  

SampleSyncAdapterColumns的地方。MIME_PROFILE = vnd.android.cursor.item/vnd.myapp.profile

我添加了一个联系人,我可以看到条目,但是当我点击它时什么也没有发生。当用户点击一个活动时,我应该怎么做才能启动它?我试图做什么是建议这里Pre-honeycomb设备:的技巧是插入一个数据行,"编辑在我的应用程序",这将带用户到你的应用程序和你的应用程序然后提供一个编辑器活动

我想你的意图过滤器可能是不正确的。根据这个条目,正确的操作和数据项应该如下所示:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.item/vnd.myapp.profile" />
</intent-filter>

我就是这么做的。在manifest文件中,我为我的一个活动添加了这些意图过滤器

<intent-filter >
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.item/vnd.myapp.profile" />
</intent-filter>
<intent-filter >
    <action android:name="android.intent.action.EDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data
        android:host="contacts"
        android:mimeType="vnd.android.cursor.item/person" />
    <data
        android:host="com.android.contacts"
        android:mimeType="vnd.android.cursor.item/contact" />
    <data
        android:host="com.android.contacts"
        android:mimeType="vnd.android.cursor.item/raw_contact" />
</intent-filter>            

当用户点击我在同步适配器帐户中添加的配置文件操作时,第一个将被广播,使用示例同步适配器中的代码(见上文)

第二个允许你在用户想要编辑联系人时处理由本地地址簿广播的意图。考虑在第一种情况下,因为mime类型是您的同步适配器之一,您的活动将被直接调用。在第二种情况下,将显示一个对话框,其中包含注册处理android.intent.action. edit的应用程序列表:mimeType="vnd.android.cursor。项目/人",android: mimeType = " vnd.android.cursor。项目/联系"等

在我的活动中,我有一个这样的方法:

boolean handleIntent(Intent intent) {
    String action = intent.getAction();
    Uri uri = intent.getData();
    if (action.equalsIgnoreCase(Intent.ACTION_VIEW)) {
        handleProfileAction(uri);  // in this case uri points to ProfileAction Data raw that is one of the Data that your sync adaoter has added in the raw contact 
    } else if (action.equalsIgnoreCase(Intent.ACTION_EDIT)) {
        editYourContact(uri); // in this case the uri points to the Contact containing you raw contact although at least on SonuEricsson  Xperia mini when this intent is broadcasted by the context menu "edit contact" command I receive the URI of the raw contact when there is only one.
    }
    return true;
}

最新更新