问题
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
回答1:
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>
回答2:
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;
}
来源:https://stackoverflow.com/questions/8344841/open-an-activity-to-edit-contact-in-sync-adapter