把提醒在实际日历在手机上

把提醒在实际日历在手机上

本文介绍了把提醒在实际日历在手机上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望把提醒在手机上的日历。换句话说,提醒需要在电话的真实日历。我们可以创建提醒与AlarmManager,但我希望它出现在日历中。如何使这在Android中?

I want to put reminders on the phone's calendar. In other words, reminders needs to be in the real calendar of the phone. We can create reminders with AlarmManager but I want it to appear in the calendar. How do I make this in Android?

推荐答案

将此code在单击按钮或在任何你想添加提醒日历

Add this code in the button click or where ever you want to add reminder to calendar

Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
                ContentResolver cr = getContentResolver();

                ContentValues values = new ContentValues();

                try
                {
                    epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch=epoch;
                    Log.e("epoch",String.valueOf(epoch));
                    epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch1=epoch1;
                    Log.e("epoch1",String.valueOf(epoch1));
                } catch (ParseException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                values.put("calendar_id", 1);
                values.put("title", "Appoitment");
                values.put("allDay", 0);
                values.put("dtstart",epoch); // event starts at 11 minutes from now
                values.put("dtend", epoch1 ); // ends 60 minutes from now
                values.put("description", "Your consulting date and time ");
                values.put("visibility", 0);
                values.put("hasAlarm", 1);
                if(EVENTS_URI!=null)
                {
                event1 = cr.insert(EVENTS_URI, values);
                }

                // reminder insert
                Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
                values = new ContentValues();
                values.put( "event_id", Long.parseLong(event1.getLastPathSegment()));
                values.put( "method", 1 );
                values.put( "minutes", 10 );
                if(REMINDERS_URI!=null)
                {
                cr.insert( REMINDERS_URI, values );
                }
                alertDialog.setTitle("Event Saved");
                Dismiss();
                alertDialog.show();

添加getCalendarUriBase功能,以您的code。

add "getCalendarUriBase" function to your code.

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;

请注意:

epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();

在这行code的日期和时间必须在你的日期以及时间您要添加remindar。

In this line of code date and time must be your date as well time to which you want to add remindar.

权限:

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

这篇关于把提醒在实际日历在手机上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:26