【问题标题】:open an activity to edit contact in sync adapter打开一个活动来编辑同步适配器中的联系人
【发布时间】:2011-12-01 16:33:46
【问题描述】:

在 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 设备执行 Here 的建议:诀窍是插入一个数据行“在 MyApp 中编辑”,这会将用户带到您的应用程序和您的应用程序 然后会提供一个编辑器活动

【问题讨论】:

    标签: android action profile android-syncadapter


    【解决方案1】:

    我认为您的意图过滤器可能不正确。根据this entry,正确的动作和数据项应该是这样的:

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

    【讨论】:

    • 但现在我需要了解 R.string.syncadapter_profile_action 是什么意思
    • 好的,我想我会用它来添加多个动作并区分 ndroid.intent.action.VIEW 意图的处理程序中的动作
    【解决方案2】:

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

    <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.intent.action.EDIT 的应用程序列表 android:mimeType="vnd.android.cursor.item/person", android:mimeType="vnd.android .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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多