gpt4 book ai didi

android - 如何在特定日历上打开日历应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:29 25 4
gpt4 key购买 nike

我想从 Android 应用程序打开日历应用程序,但使用特定的 calendarId

我的代码:

final Intent calIntent = new Intent(Intent.ACTION_EDIT);
calIntent.setType("vnd.android.cursor.item/event");

startActivityForResult(calIntent, RESULT_CODE_OPEN);

但 Intent 打开时使用的是上次使用的日历。我想用他的日历 ID 打开一个特定的日历。

我该怎么做?

最佳答案

一点历史: 查看日历应用程序源代码,我发现 list 的 this section 指示哪个类处理 Intent.ACTION_EDIT,EditEventActivity.java。然后我找到了this线; calendarId 从额外数据中获取。所以……

打开一个 EditEvent - (原始问题的答案)

使用键 CalendarContract.Events.CALENDAR_ID 和相应的 calendarId 在 Intent 上放置一个额外的 long 就像这个示例:

long calendarId = 1234; // here goes your calendar Id

final Intent calIntent = new Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
.putExtra(CalendarContract.Events.CALENDAR_ID, calendarId);

startActivityForResult(calIntent, RESULT_CODE_OPEN);

在特定日历上打开带有 周 View 的日历应用程序 - (回答赏金问题 - 第 1 部分)

这种方法有点不同。在打开日历应用程序之前,您应该将所有日历设置为不可见,但不要选择一个。因此,我创建了一个辅助函数来执行此操作。

private void makeAllCalendarsInvisibleExcept(long calendarId) {
ContentValues updateValues = new ContentValues();
updateValues.put(CalendarContract.Calendars.VISIBLE, 0);

// make all invisible
getContentResolver().update(CalendarContract.Calendars.CONTENT_URI,
updateValues, null, null);

updateValues.clear();
updateValues.put(CalendarContract.Calendars.VISIBLE, 1);


// make calendarId visible
getContentResolver().update(CalendarContract.Calendars.CONTENT_URI,
updateValues, where, null);
}

这里是如何设置它的:

makeAllCalendarsInvisibleExcept(calendarId);
Intent calIntent = new Intent();
// for google calendar, first parameter should be "com.google.android.calendar"
// this is intended for android calendar.
ComponentName cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
calIntent.setComponent(cn);
startActivityForResult(calIntent, 4567);

据我所知,无法打开月 View 。但研究永无止境,我会尽快进行更新。

在 DemoApp 中,我创建了两个额外的辅助方法来推送和恢复日历可见性状态。看看 pushCalendarVisibilityrestoreCalendarVisibilityonActivityResult

注意:我已经在安装和未安装 Google 日历的情况下测试了该应用程序。 Google 日历上的 AFAIK 仅显示 DAY View ,而不是 WEEK 和 MONTH。

在所有情况下,仅显示选定日历上的事件。

获取 calendarIdAccount Name - (赏金问题的答案 - 第 2 部分)

calendarId abd Account Name 可以使用 CalendarContract.Calendars 获取,如下所示:

// since API14 
Uri uri = CalendarContract.Calendars.CONTENT_URI;

String[] projection = new String[] {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.VISIBLE,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME,
};

Cursor calendarCursor = getContentResolver().query(uri, projection, null, null, null);
while (calendarCursor.moveToNext()) {

// calendarId
long calendarId = calendarCursor.getLong(0);

// get visibility, warning: if calendar is not visible Intent.ACTION_EDIT doesn't work.
boolean calendarVisible = calendarCursor.getInt(1) == 1;

// get if it's owned, warning: if calendar is not owned Intent.ACTION_EDIT doesn't work.
boolean calendarOwned = calendarCursor.getInt(2) == CalendarContract.Calendars.CAL_ACCESS_OWNER;

// get calendar display name
String calendarName = calendarCursor.getString(3);

// get account name
String accountName = calendarCursor.getString(4);

// do something

}

注意:日历投影有更多可用选项,建议阅读CalendarContract.Calendars

限制

此功能自 API 级别 14、Android 4.0 (ICE_CREAM_SANDWICH) 起可用

演示应用

this GitHub repository 中的完整示例。 (Android Studio 1.0.0 项目)要求同步日历并且必须由帐户拥有并且必须可见。更新:示例是最新的

在 Android 4.1.1 和 Android 5.0 Lollipop Preview 上测试

关于android - 如何在特定日历上打开日历应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com