5

我想从我的应用程序中打开日历并将参数“日期”传递给这个日历。

这个日历将显示日期的相应日期页面。

我调查了 calendar 的源代码,但没有找到使用方法。

public static void openCalendarApp(Context context)
{   
    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.android.calendar");
    context.startActivity(intent);
}
4

2 回答 2

4

您可以使用日历视图来执行此操作...使用该setDate(long date)方法将日历上的日期设置为您想要的日期

您也可以通过像这样将事件添加到日历来做到这一点

创建日历的意图

Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(calIntent)

播种日历日期和时间

Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(Events.TITLE, "Title here");
calIntent.putExtra(Events.EVENT_LOCATION, "Location here");
calIntent.putExtra(Events.DESCRIPTION, "Description here");
GregorianCalendar calDate = new GregorianCalendar(2012, 7, 15);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
     calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
     calDate.getTimeInMillis());
startActivity(calIntent);

可以在这里看到一个例子

于 2012-12-21T10:38:03.573 回答
0

以下是同时支持新旧版本 Android 的方法:

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static Intent prepareIntentForCalendar(final Context context, final Date date) {
    Intent intent = null;
    if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
        // go to date of the calendar app, as shown here:
        // http://developer.android.com/guide/topics/providers/calendar-provider.html#intent-view
        final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
        builder.appendPath("time");
        ContentUris.appendId(builder, date.getTime());
        intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
        final PackageManager pm = context.getPackageManager();
        final ResolveInfo resolveActivity = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY
                | PackageManager.GET_RESOLVED_FILTER);
        if (resolveActivity == null)
            return null;
    } else {
        intent = new Intent(Intent.ACTION_EDIT);
        intent.setClassName("com.android.calendar", "com.android.calendar.AgendaActivity");
        intent.putExtra("beginTime", date.getTime());
        final PackageManager pm = context.getPackageManager();
        final ResolveInfo resolveActivity = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY
                | PackageManager.GET_RESOLVED_FILTER);
        if (resolveActivity == null)
            intent = null;
    }
    if (intent != null)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY
                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    return intent;
}
于 2014-11-09T10:51:31.043 回答