使用意图在月视图中显示日历
问题描述:
我正在开发一个小部件,该小部件在使用某些用户操作时会打开使用意图的常规Android日历。我目前正在开发ICS,所以不要太关注API的旧版本。我可以用下面的代码打开日视图:使用意图在月视图中显示日历
Intent intent2 = new Intent();
intent2.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.AllInOneActivity"));
intent2.setAction("android.intent.action.MAIN");
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setFlags(0x10200000);
intent2.putExtra("beginTime", dateStartMillis);
intent2.putExtra("VIEW", "DAY");
context.startActivit(intent2);
不过,我似乎无法找到一种方法,在月视图中打开它。根据AllInOneActivity的GrepCode,在其onCreate
方法中,它调用Utils.getViewTypeFromIntentAndSharedPref(this);
来确定要显示哪个视图。下面是一个方法:
public static int getViewTypeFromIntentAndSharedPref(Activity activity) {
Intent intent = activity.getIntent();
Bundle extras = intent.getExtras();
SharedPreferences prefs = GeneralPreferences.getSharedPreferences(activity);
if (TextUtils.equals(intent.getAction(), Intent.ACTION_EDIT)) {
return ViewType.EDIT;
}
if (extras != null) {
if (extras.getBoolean(INTENT_KEY_DETAIL_VIEW, false)) {
// This is the "detail" view which is either agenda or day view
return prefs.getInt(GeneralPreferences.KEY_DETAILED_VIEW,
GeneralPreferences.DEFAULT_DETAILED_VIEW);
} else if (INTENT_VALUE_VIEW_TYPE_DAY.equals(extras.getString(INTENT_KEY_VIEW_TYPE))) {
// Not sure who uses this. This logic came from LaunchActivity
return ViewType.DAY;
}
}
// Default to the last view
return prefs.getInt(
GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
}
我没有在这个方法中看到(或其他任何地方为此事)的方式来设置视图MonthView。是否有某种我可以使用的技巧,或者我应该接受这是不可能的?
答
从这里: Android Developer Calendar Provider Docs
日历提供商提供了两种不同的方式使用VIEW意图:
要打开日历特定日期。 查看活动。 下面是显示了如何打开日历到特定的日期的示例:
// 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);
这也意味着,没有官方的方式来确定显示哪个视图。您的代码仅适用于ICS中的特定日历应用程序,并且很可能不适用于大多数其他应用程序。
“我可以用下面的代码打开日视图” - 该代码是可怕的。您是日历应用程序的一次轻微更新,远离那次突破。 – CommonsWare 2013-05-07 16:47:58
@CommonsWare我并不反对,但是考虑到这是基于旧的Android 2.1代码,它并没有那么糟糕。我希望标准的Android日历提供了更多的方式将其发布到特定的视图... – 2013-05-07 16:51:38
对于那些在这篇文章中磕磕绊绊,想知道是否有更好的方式来打开特定日期的日历应用程序,我发现这一点:http:// developer .android.com /引导/主题/供应商/日历provider.html#意向视图。希望这会帮助其他人;) – 2Dee 2013-11-27 20:16:43