如何在Android中设置提醒?

我正在尝试为Android构build一个日历应用程序。 我感到中间。 我设法从用户​​那里获取诸如时间和任务的信息。

我不知道如何将这个添加到android事件。 有没有像setEvent或类似的东西?

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

你有几个select,

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

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

    或者更复杂的一个:

  2. 使用此方法获取日历的引用
    强烈build议不要使用这种方法,因为它可能会破坏较新的Android版本):

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

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

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

更新:ICS问题

上面的例子使用了未公开的日历API,ICS已经发布了新的公共日历API,所以为了定位新的Android版本,你应该使用CalendarContract 。

有关这方面的更多信息可以在这篇博文中find。

您可以使用AlarmManager与通知机制一起如下所示:

 Intent intent = new Intent(ctx, ReminderBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE); // time of of next reminder. Unix time. long timeMs =... if (Build.VERSION.SDK_INT < 19) { am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent); } else { am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent); } 

它开始报警。

 public class ReminderBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(...) .setContentTitle(..) .setContentText(..); Intent intentToFire = new Intent(context, Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentToFire, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); NotificationManagerCompat.from(this);.notify((int) System.currentTimeMillis(), builder.build()); } } 

Android完整的源代码添加事件和提醒。

 /** Adds Events and Reminders in Calendar. */ private void addReminderInCalendar() { Calendar cal = Calendar.getInstance(); Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events"); ContentResolver cr = getContentResolver(); TimeZone timeZone = TimeZone.getDefault(); /** Inserting an event in calendar. */ ContentValues values = new ContentValues(); values.put(CalendarContract.Events.CALENDAR_ID, 1); values.put(CalendarContract.Events.TITLE, "Sanjeev Reminder 01"); values.put(CalendarContract.Events.DESCRIPTION, "A test Reminder."); values.put(CalendarContract.Events.ALL_DAY, 0); // event starts at 11 minutes from now values.put(CalendarContract.Events.DTSTART, cal.getTimeInMillis() + 1 * 60 * 1000); // ends 60 minutes from now values.put(CalendarContract.Events.DTEND, cal.getTimeInMillis() + 2 * 60 * 1000); values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); values.put(CalendarContract.Events.HAS_ALARM, 1); Uri event = cr.insert(EVENTS_URI, values); // Display event id. Toast.makeText(getApplicationContext(), "Event added :: ID :: " + event.getLastPathSegment(), Toast.LENGTH_SHORT).show(); /** Adding reminder for event added. */ Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(true) + "reminders"); values = new ContentValues(); values.put(CalendarContract.Reminders.EVENT_ID, Long.parseLong(event.getLastPathSegment())); values.put(CalendarContract.Reminders.METHOD, Reminders.METHOD_ALERT); values.put(CalendarContract.Reminders.MINUTES, 10); cr.insert(REMINDERS_URI, values); } /** Returns Calendar Base URI, supports both new and old OS. */ private String getCalendarUriBase(boolean eventUri) { Uri calendarURI = null; try { if (android.os.Build.VERSION.SDK_INT <= 7) { calendarURI = (eventUri) ? Uri.parse("content://calendar/") : Uri.parse("content://calendar/calendars"); } else { calendarURI = (eventUri) ? Uri.parse("content://com.android.calendar/") : Uri .parse("content://com.android.calendar/calendars"); } } catch (Exception e) { e.printStackTrace(); } return calendarURI.toString(); } 

将权限添加到您的清单文件。

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