今天是android的CotentProvider的Calendar的最後一節,今天接着上節,講使用Intent查看日曆,以及同步適配器。
以下是android官方講解:
使用 Intent 查看日曆數據
日曆提供程序提供了兩種不同的 VIEW Intent 使用方法:
打開日曆並定位到特定日期。
查看事件。
下例顯示如何打開日曆並定位到特定日期:
// A date-time specified in milliseconds since the epoch.
long startMillis;
...
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, startMillis);
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(builder.build());
startActivity(intent);
下例顯示如何打開事件進行查看:
long eventID = 208;
...
Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(uri);
startActivity(intent);
同步適配器
應用和同步適配器在訪問日曆提供程序的方式上只存在微小差異:
- 同步適配器需要通過將 CALLER_IS_SYNCADAPTER 設置爲 true 來表明它是同步適配器。
- 同步適配器需要提供 ACCOUNT_NAME 和 ACCOUNT_TYPE 作爲 URI 中的查詢參數。
- 與應用或小部件相比,同步適配器擁有寫入權限的列更多。 例如,應用只能修改日曆的少數幾種特性,例如其名稱、顯示名稱、能見度設置以及是否同步日曆。 相比之下,同步適配器不僅可以訪問這些列,還能訪問許多其他列,例如日曆顏色、時區、訪問級別、地點等等。不過,同步適配器受限於它指定的 ACCOUNT_NAME 和 ACCOUNT_TYPE。
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();
}
如需查看同步適配器的實現示例(並非僅限與日曆有關的實現),請參閱 SampleSyncAdapter。