如何在Android中设置提醒

如何在Android中设置提醒

本文介绍了如何在Android中设置提醒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Android 构建一个日历应用程序.我被击中了中间.我已经设法从用户​​那里检索到时间和任务等信息.

I am trying to build a calendar app for Android. I am struck in the middle. I have managed to retrieve the information such as time and task from the user.

我不知道如何将它添加到 android 事件中.有没有类似 setEvent 或类似的东西?

I don't know how to add this into android events. Is there anything like setEvent or something similar?

推荐答案

不,如果你想透明地添加到用户的日历中,它比仅仅调用一个方法更复杂.

Nope, it is more complicated than just calling a method, if you want to transparently add it into the user's calendar.

你有几个选择;

  1. 调用意图在日历上添加事件
    这将弹出日历应用程序并让用户添加事件.您可以传递一些参数来预填充字段:

  1. Calling the intent to add an event on the calendar
    This will pop up the Calendar application and let the user add the event. You can pass some parameters to prepopulate fields:

Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

或者更复杂的一个:

使用此方法获取日历的引用
(强烈建议不要使用此方法,因为它可能会在较新的 Android 版本上中断):

Get a reference to the calendar with this method
(It is highly recommended not to use this method, because it could break on newer Android versions):

private String getCalendarUriBase(Activity act) {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase;
}

并以这种方式添加事件和提醒:

// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );

您还需要将这些权限添加到此方法的清单中:

You'll also need to add these permissions to your manifest for this method:

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

更新:ICS 问题

以上示例使用未记录的日历 API,新的公共日历 API 已针对 ICS 发布,因此,针对新的 android 版本,您应该使用 CalendarContract.

The above examples use the undocumented Calendar APIs, new public Calendar APIs have been released for ICS, so for this reason, to target new android versions you should use CalendarContract.

更多信息可以在 这篇博文中找到.

这篇关于如何在Android中设置提醒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:58