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

发布于 2024-12-18 22:30:26 字数 1465 浏览 3 评论 0原文

在 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

我添加了一个联系人,我可以看到该条目,但是当我点击它什么也没有发生。当用户单击某个活动时,我应该怎么做才能启动该活动? 我试图对预蜂窝设备执行此处的建议: 技巧是插入一个数据行“在 MyApp 中编辑”,这会将用户带到您的应用程序和您的应用程序 然后将提供编辑器活动

In the Android SampleSyncAdapter there is the following piece of code:

/**
 * 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;
}

I added this as filter for my activity

    <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>  

where SampleSyncAdapterColumns.MIME_PROFILE = vnd.android.cursor.item/vnd.myapp.profile

I added a contact and I can see the entry but when I click on it nothing happens. What should I do to start an activity when the user clicks on it?
I was trying to do what is suggested Here for Pre-honeycomb devices: The trick is to insert a data row, "Edit in MyApp", which would take the user to your app and your app
would then provide an editor activity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-12-25 22:30:26

我认为您的意图过滤器可能不正确。根据此条目,正确的操作和数据项应该类似于以下内容:

<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>

I think your intent filter might be incorrect. According to this entry, the correct action and data items should be something like the following:

<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>
无所的.畏惧 2024-12-25 22:30:26

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

<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>            

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

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

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

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;
}

This is what I did.
In the manifest file I added these intent filters for one of my activity

<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>            

The first one will be broadcasted when the user clicks on the profile action that I added in my sync adapter accounts using the code in the sample sync adapter (see above)

The second one allows you to handle the intent that is boradcasted by the native address book when the user wants to edit the contact.
Consider that in the first case because the mimetype is that one of your syncadapter your activity will be called directly.
In the second case a dialog will be shown with the list of applications registered to handle the android.intent.action.EDIT for android:mimeType="vnd.android.cursor.item/person", android:mimeType="vnd.android.cursor.item/contact" etc

In my activity I have a method like this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文