您需要按照以下步骤进行操作:
the class you use for saving events with extended propeties should
extends AbstractThreadedSyncAdapter , then implements the method onPerfomSync(...)
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
System.out.println("Sync......");
saveEvent();
}
在同一个类中添加以下方法:
static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
}
创建一个继承
Service 类的类,如下所示。
public class SyncService extends Service {
private static final String TAG = "SyncService";
private static final Object sSyncAdapterLock = new Object();
private static EditEventHelper sSyncAdapter = null;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service created");
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new EditEventHelper(getApplicationContext());
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroyed");
}
@Override
public IBinder onBind(Intent intent) {
return sSyncAdapter.getSyncAdapterBinder();
}
在 res 路径下创建一个名为 syncadpater.xml 的 xml 文件,其内容如下:
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.android.calendar"
android:accountType="com.android.google"
android:userVisible="true"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="false"
/>
用于向您的事件添加扩展属性的代码将是:
ContentValues customerContentValues_1 = new ContentValues();
customerContentValues_1.put(ExtendedProperties.EVENT_ID, model.mId);
customerContentValues_1.put(ExtendedProperties.NAME, "name");
customerContentValues_1.put(ExtendedProperties.VALUE, value);
activity.getContentResolver().insert(asSyncAdapter(ExtendedProperties.CONTENT_URI, mOwnerAccount, ACCOUNT_TYPE), customerContentValues_1);
在
AndroidManifest.xml 文件中添加以下权限:
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<!
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<!
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
然后在与syncadapter.xml文件相关联的清单文件中声明您创建的服务:
<service
android:name="com.android.calendar.iselection.event.SyncService"
android:exported="true" >
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
祝你好运!