Android插入新的日历同步

时间:2013-04-07 17:20:48

标签: android google-calendar-api android-calendar android-syncadapter

我会使用自定义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创建新日历的正确解决方案是什么? 最后有疑问:有可能将新日历与Google日历同步吗? 提前致谢

0 个答案:

没有答案