为什么CalendarProvider不允许编写ExtendedProperties? [英] Why CalendarProvider doesn't allow writing ExtendedProperties?

查看:313
本文介绍了为什么CalendarProvider不允许编写ExtendedProperties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google日历事件具有扩展的属性,可用于将名称/值对附加到事件.

Google calendar events have extended properties that can be used to attach name/value pairs to an event.

我们正在实现一个协作日历应用程序,该应用程序使用这些扩展的属性将更多信息附加到事件中.根据Google的建议,我们使用Android CalendarProvider读取和创建新事件.当我们创建一个新事件时,我们需要向其添加一些扩展属性,但我们只是意识到,日历提供程序不允许编写CalendarContract.ExtendedProperties,如果尝试这样做,则会出现以下错误:

We are implementing a collaborative calendar application that uses those extended properties to attach extra information to the event. As recommended by Google, we use the Android CalendarProvider to read and create new events. When we create a new event we need to add some extended properties to it but we just realised that the calendar provider doesn't allow writing CalendarContract.ExtendedProperties, if we try we get the following error:

Only sync adapters may write using content://com.android.calendar/extendedproperties

在CalendarProvider中这些属性是只读的似乎有点奇怪,因为它破坏了它们能够将一些额外的元数据附加到事件上的全部目的.

It seems a bit weird that these properties are read-only in the CalendarProvider because it defeats the whole purpose of them that is being able to attach some extra metadata to the event.

有人知道此限制的解决方法吗?

Does anyone know a workaround for this limitation?

推荐答案

您必须按照以下步骤操作:

you have to proceed as below :

  • 用于保存具有扩展属性的事件的类应 扩展 AbstractThreadedSyncAdapter ,然后实现方法 onPerfomSync(...)

  • 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();//your saving events method... 

}

将以下方法添加到同一类中:

add the method below in the same class :

    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 类的类,如下所示

create a class that extends the Service class like below

public class SyncService extends Service {
private static final String TAG = "SyncService";

private static final Object sSyncAdapterLock = new Object();
private static EditEventHelper sSyncAdapter = null;

/**
 * Thread-safe constructor, creates static {@link SyncAdapter} instance.
 */
@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Service created");
    synchronized (sSyncAdapterLock) {
        if (sSyncAdapter == null) {
            sSyncAdapter = new EditEventHelper(getApplicationContext());

        }
    }
}

@Override
/**
 * Logging-only destructor.
 */
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "Service destroyed");
}

/**
 * Return Binder handle for IPC communication with {@link SyncAdapter}.
 *
 * <p>New sync requests will be sent directly to the SyncAdapter using this channel.
 *
 * @param intent Calling intent
 * @return Binder handle for {@link SyncAdapter}
 */
@Override
public IBinder onBind(Intent intent) {
    return sSyncAdapter.getSyncAdapterBinder();
}

}

res 路径中,创建一个具有以下内容的xml文件 syncadpater.xml :

In the res path create an xml file syncadpater.xml with the content :

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

用于向您的事件添加Extendedproperties的代码将是:

The code for used for adding an Extendedproperties to you event, will be :

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 文件中,添加以下权限:

In the AndroidManifest.xml file add these permissions :

    <uses-permission android:name="android.permission.READ_SYNC_STATS" />
<!-- Required to enable our SyncAdapter after it's created. -->
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<!-- Required because we're manually creating a new account. -->
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />

然后在与syncadapter.xml文件关联的清单文件中声明您已经创建的服务:

Then declare the Service that you've create into the manifest file associated to the syncadapter.xml file:

        <service
        android:name="com.android.calendar.iselection.event.SyncService"
        android:exported="true" >

        <!--
        This intent filter is required. It allows the system to launch our sync service
        as needed.
        -->
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <!-- This points to a required XML file which describes our SyncAdapter. -->
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>

祝你好运!

这篇关于为什么CalendarProvider不允许编写ExtendedProperties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆