如何使用 Intent (Froyo) 启动 Android 日历应用程序

发布于 2024-10-07 00:35:31 字数 116 浏览 3 评论 0原文

我想从 Android 应用程序打开日历应用程序。当我在线搜索时,我得到的只是使用意图创建新事件。我可以找到打开联系人和图库等的意图。

是否可以将日历启动到特定的一周或一天?如果可能的话,有人可以帮我吗?

I want to open up the Calendar application from an android application. When i searched online, all i got is to create new events using intent. I could find Intents to open Contacts and Gallery etc.

Is it possible to launch the Calendar to a specific week or day? If possible, could someone please help me with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟燃烟灭 2024-10-14 00:35:31
Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", eventStartInMillis);
intent.putExtra("endTime", eventEndInMillis);
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", eventStartInMillis);
intent.putExtra("endTime", eventEndInMillis);
startActivity(intent);
又怨 2024-10-14 00:35:31

您可以通过两种方式打开日历视图:

1) 按特定日期
2) 按特定事件

为此,您必须添加以下两个权限

  <uses-permission android:name="android.permission.READ_CALENDAR" />
  <uses-permission android:name="android.permission.WRITE_CALENDAR" />

1) 按特定日期:

Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(Intent.ACTION_VIEW)
    .setData(builder.build());
startActivity(intent);

2) 按特定事件:

long eventID = 200;

Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(intent);

注意:CalendarContract 内容提供程序已添加到 API 级别 14 的 Android SDK 中。更多信息,您可以访问此链接

You can open Calendar View by two means:

1) by particular date
2) by particular event

For this you have to add following two permissions

  <uses-permission android:name="android.permission.READ_CALENDAR" />
  <uses-permission android:name="android.permission.WRITE_CALENDAR" />

1) by particular date:

Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(Intent.ACTION_VIEW)
    .setData(builder.build());
startActivity(intent);

2) by particular event:

long eventID = 200;

Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(intent);

Note: CalendarContract content provider was added to the Android SDK in API Level 14. For more information you can visit this link

简单 2024-10-14 00:35:31

查看Android源代码中的日历应用程序后,您只能直接调用AgendaActivity。其他的将不起作用。正如其他发帖者所指出的,您可以直接与光标交互来读取/创建事件,但您无法将日历应用程序调用到 AgendaView 之外的视图。原因是开发人员通过使用以下活动定义在 Cal 应用程序的清单中限制了该功能:

    <activity android:name="MonthActivity" android:label="@string/month_view"
        android:theme="@style/CalendarTheme" />
    <activity android:name="WeekActivity" android:label="@string/week_view"
        android:theme="@style/CalendarTheme" />
    <activity android:label="@string/day_view" android:name="DayActivity"     
        android:theme="@style/CalendarTheme"/>
    <activity android:name="AgendaActivity" android:label="@string/agenda_view"
        android:theme="@android:style/Theme.Light"
        android:exported="true" />

请注意,只有 AgendaActivity 具有 android:exported="true"。如果您尝试调用其他活动,您将收到权限异常。

但是,您可以使用以下代码将 AgendaActivity 调用到任意一天:

    Calendar tempCal = (Calendar) mCalendar.clone();
    tempCal.set(year, month, day); 
    Intent calendarIntent = new Intent() ;
    calendarIntent.putExtra("beginTime", tempCal.getTimeInMillis());
    calendarIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");
    startActivity(calendarIntent);

After reviewing the Calendar App in the Android source code you can only invoke the AgendaActivity directly. The others will not work. As the other posters have noted you can interact directly with the cursor to read/create events, but you can't invoke the calendar app to a view other than the AgendaView. The reason is that the developers have limited that ability in the manifest for the Cal app by using the following activity definitions:

    <activity android:name="MonthActivity" android:label="@string/month_view"
        android:theme="@style/CalendarTheme" />
    <activity android:name="WeekActivity" android:label="@string/week_view"
        android:theme="@style/CalendarTheme" />
    <activity android:label="@string/day_view" android:name="DayActivity"     
        android:theme="@style/CalendarTheme"/>
    <activity android:name="AgendaActivity" android:label="@string/agenda_view"
        android:theme="@android:style/Theme.Light"
        android:exported="true" />

Note that only the AgendaActivity has android:exported="true". If you attempt to call the other activities you will get a permission exception.

However, you can invoke the AgendaActivity to an arbitrary day with the following code:

    Calendar tempCal = (Calendar) mCalendar.clone();
    tempCal.set(year, month, day); 
    Intent calendarIntent = new Intent() ;
    calendarIntent.putExtra("beginTime", tempCal.getTimeInMillis());
    calendarIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");
    startActivity(calendarIntent);
满栀 2024-10-14 00:35:31

使用 Intent 查看日历数据

Calender Provider 提供了两种不同的方式来使用 VIEW Intent:

To open the Calendar to a particular date.
To view an event.

将权限添加到清单

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

下面是一个示例,展示了如何打开特定日期的日历:

 // 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);

是否可以启动特定周或某一天的日历?

因此,现在可以,但需要最低 API 14

有关更多详细信息,请访问 http://developer.android.com/guide /topics/providers/calendar-provider.html#intents

Using intents to view calendar data

Calender Provider offers two different ways to use the VIEW Intent:

To open the Calendar to a particular date.
To view an event.

add permissions to manifest

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

Here is an example that shows how to open the Calendar to a particular date:

 // 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);

Here is an example that shows how to open an event for viewing:

long eventID = 208;
...
Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW)
   .setData(uri);

    startActivity(intent);

Is it possible to launch the Calendar to a specific week or day?

So, now it is possible, but requires min API 14.

For more details visit http://developer.android.com/guide/topics/providers/calendar-provider.html#intents

蛮可爱 2024-10-14 00:35:31

AgendaActivity 加载“议程”视图。

根据我的经验,您无法深入链接到 Android 中的日、周和月活动,但是您可以使用“LaunchActivity”来加载上次打开的视图。

AgendaActivity loads the "Agenda" view.

From my experience you can't deep link into the Day, Week and Month activities in stock Android, however you can use "LaunchActivity" which loads the last opened view.

清秋悲枫 2024-10-14 00:35:31

您可以使用以下代码,如果您的设备上安装了多个日历应用程序,您还可以选择一个日历应用程序

fun openCalendarApp(activity: Activity?) {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
    activity?.startActivity(intent)
}

You can use the below code, which also enables you to choose a calendar app if there are multiple calendar apps installed on your device

fun openCalendarApp(activity: Activity?) {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
    activity?.startActivity(intent)
}
奢华的一滴泪 2024-10-14 00:35:31

通过繁琐的实验过程,我发现:

Intent calendarIntent = new Intent() ;
calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");

...适合我启动日历的议程活动。没有
尝试过,但也许是 com.android.calendar.DayActivity,
.WeekActivity,和.MonthActivity会启动相应的
活动。

By a process of tedious experimentation, I've found that:

Intent calendarIntent = new Intent() ;
calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");

...works for me to launch the Calendar's Agenda Activity. Haven't
tried, but perhaps com.android.calendar.DayActivity,
.WeekActivity,and .MonthActivity will launch the corresponding
Activities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文