android - 将多个参数放入 ContentResolver.requestSync

标签 android serialization android-intent android-contentresolver android-syncadapter

我正在制作一个具有 syncAdapter 的应用。

我希望能够执行 requestSync 以及一些将通过其 bundle 发送的参数。原因是我希望选择 syncAdapter 实例将执行的操作。

出于某种原因,putSerializable 和 putIntArray 都会导致 ContentResolver 抛出异常:

08-16 14:34:49.080: E/AndroidRuntime(10318): java.lang.IllegalArgumentException: unexpected value type: java.util.MiniEnumSet
08-16 14:34:49.080: E/AndroidRuntime(10318):    at android.content.ContentResolver.validateSyncExtrasBundle(ContentResolver.java:1144)
08-16 14:34:49.080: E/AndroidRuntime(10318):    at android.content.ContentResolver.requestSync(ContentResolver.java:1111)
08-16 14:34:49.080: E/AndroidRuntime(10318):    at com.sciatis.syncer.syncing.SyncAdapter.requestSync(SyncAdapter.java:100)
08-16 14:34:49.080: E/AndroidRuntime(10318):    at 
...

为什么会这样?有办法克服这个吗?放一个整数工作正常但那些操作没有。

我做错了什么吗?有没有更好的方法来实现从 Activity 向 syncAdapter 发送参数?

最佳答案

ContentResolver.requestSync说:

Only values of the following types may be used in the extras bundle: Integer Long Boolean Float Double String

在那种情况下你可以尝试:

Bundle extras = new Bundle(); 
int[] arr = new int[] {1,2,3,4};
extras.putInt("arrlen", arr.length);
for (int i = 0; i < arr.length; i++) { 
  extras.putInt("arr"+ i, arr[i]);
} 

然后在 SyncAdapter 中读取这些值:

Bundle extras; //taken from method params
int[] arr = new int[extras.getInt("arrlen")];
for (int i = 0; i < arr.length; i++) { 
  arr[i] = extras.getInt("arr"+ i);
} 

关于android - 将多个参数放入 ContentResolver.requestSync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11986331/

相关文章:

android - 寻求mp4的Exoplayer删除缓冲区

java - 将对象从 java 传输到 C#

android - 如何更改在 Google App Inventor 中创建的 Android 应用程序的图标?

android - 动态创建 View 时出现问题(在 Android 中)[错误 : couldn't save which view . ..]

android - 尽管我使用的是 LinearLayout,为什么我的 2 个自定义 View 布局彼此重叠

android - 位置无关的可执行文件和 Android

c# - 无法序列化 session 状态... [已更新!]

c# - 将属性名称有空格的json对象从c#发布到第三方api

android - 为什么从最近的应用程序返回 Activity 时会重新传递 Intent ?

android - Sqlite 游标窗口分配 2048 kb 失败