android developer tiny share-20170620

来源:互联网 发布:mac上哪里下游戏 编辑:程序博客网 时间:2024/06/10 22:52

今天是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。
您可以利用以下帮助程序方法返回供与同步适配器一起使用的 URI:

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。

原创粉丝点击