3

我将使用我的自定义 Android 应用程序在现有的 Google 帐户中创建一个新日历。按照堆栈溢出中的一些教程,我创建了由以下组件组成的应用程序:主要活动(我在其中读取和管理事件)、一个服务文件(称为 CalendarService)和一个 SyncAdapter。在主要活动中,我使用 Intent 初始化了服务:

private Intent calendarServiceIntent;
...
calendarServiceIntent = new Intent(getApplicationContext(), CalendarService.class);
startService(calendarServiceIntent);

然后我以这种方式实现了服务:

public class CalendarService extends Service{

//private static final Object sSyncAdapterLock = new Object();

private static CalendarSyncAdapter sSyncAdapter = null;

@Override
public void onCreate() {
    super.onCreate();
    //synchronized (sSyncAdapterLock) {
        if (sSyncAdapter == null) {
            System.out.println("SERVICE STARTED ON CREATE");
            sSyncAdapter = new CalendarSyncAdapter(getApplicationContext(), true); 
        }
   // }
}

@Override
public IBinder onBind(Intent intent) {
    System.out.println("SERVICE STARTED BINDER");
    return sSyncAdapter.getSyncAdapterBinder();
}

}

然后我实现类 CalendarSyncAdapter 扩展 AbstractThreadedSyncAdapter 为:

    public CalendarSyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
}

@Override
public void onPerformSync(Account account, Bundle extras, String authority,
        ContentProviderClient provider, SyncResult syncResult) {
    Uri calUri = CalendarContract.Calendars.CONTENT_URI;
      ContentValues cv = new ContentValues();
      cv.put(CalendarContract.Calendars.ACCOUNT_NAME, "mygoogleaccount@gmail.com");
      cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
      cv.put(CalendarContract.Calendars.NAME, "MyCalendar");
      cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "MyCalendar");
      cv.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.RED);
      cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
      cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, "mygoogleaccount@gmail.com");
      cv.put(CalendarContract.Calendars.VISIBLE, 1);
      cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

      ContentResolver cr = getContext().getContentResolver();

      Uri creationUri = asSyncAdapter(Calendars.CONTENT_URI, "mygoogleaccount@gmail.com", CalendarContract.ACCOUNT_TYPE_LOCAL );
      Uri created = cr.insert( creationUri, cv );

}



private Uri asSyncAdapter( Uri uri, String account, String accountType )
{
    return uri.buildUpon()
    .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER,"true")
    .appendQueryParameter(Calendars.ACCOUNT_NAME, "mygoogleaccount@gmail.com")
    .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL).build();
}

这是 xml 文件的一部分

<service android:name=".CalendarService"
android:exported="true">
<intent-filter>
<action android:name="android.content.CalendarSyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.CalendarSyncAdapter"
android:resource="@xml/calendar_sync_adapter" />
</service> 

这是关联的 xml 源

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
   android:contentAuthority="com.android.calendar"
   android:accountType="com.google"
   android:userVisible="true"
/>

显然,这一切都不起作用,只有当我在主要活动中移动代码(在方法“onPeformSync”中)时才会创建新日历。哪个是使用 syncAdapter 创建新日历的正确解决方案?最后一个疑问:有没有可能将新日历与谷歌日历同步?提前致谢

4

0 回答 0