我们如何向日历添加提醒

时间:2014-01-09 19:01:32

标签: android-calendar

我正在尝试将开始日期的事件添加到结束日期,我已经做了很多googleing,     但即时面临的错误,我也在我的mainfest文件中添加权限。问题出在我的代码中执行URI ...我写了很多评论行..因为我试图通过Uri,但我仍然是得到error.ie读取异常并插入查询..请帮助我。 提前致谢

package com.example.salesworld;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.util.EventLog.Event;
import android.widget.Toast;

public class PushReminder {
Activity activity;
int sYear,sMonth,sDay,eYear,eMonth,eDay;

public PushReminder() {
    // TODO Auto-generated constructor stub
}

public PushReminder(String Start_date, String end_date,Activity ac) {
    // TODO Auto-generated constructor stub
    activity=ac;




addReminder(Start_date,end_date);



}
public void addReminder(String start,String end)
{
    int shour=10;
    int smin =30;
    int ehour=18;
    int emin =30;
    ContentResolver cr=activity.getContentResolver();

    Calendar beginTime=Calendar.getInstance();
    String[] sDat=start.split("-");
    String[] eDat=end.split("-");
     sDay=Integer.parseInt(sDat[0]);
    sMonth=Integer.parseInt(sDat[1]);
    sYear=Integer.parseInt(sDat[2]);

     eDay=Integer.parseInt(eDat[0]);
        eMonth=Integer.parseInt(eDat[1]);
        eYear=Integer.parseInt(eDat[2]);


    beginTime.set(sYear, sMonth, sDay,shour,smin);
    long startTime=beginTime.getTimeInMillis();
    Calendar endTime=Calendar.getInstance();
    endTime.set(eYear,eMonth,eDay,ehour,emin);
    long end1=endTime.getTimeInMillis();
    Uri uri = CalendarContract.Calendars.CONTENT_URI;
    String[] projection = new String[] {
           CalendarContract.Calendars._ID,
           CalendarContract.Calendars.ACCOUNT_NAME,
           CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
           CalendarContract.Calendars.NAME,
           CalendarContract.Calendars.CALENDAR_COLOR
    };

    //Cursor calendarCursor = managedQuery(uri, projection, null, null, null);




    Cursor cursor = activity.getContentResolver() .query(uri,projection, null,null, null);
     /*cursor.moveToFirst();
      // fetching calendars name
      String CNames[] = new String[cursor.getCount()];
      // fetching calendars id
      int[] CalIds = new int[cursor.getCount()];
      for (int i = 0; i < CNames.length; i++) {
       CalIds[i] = cursor.getInt(0);
       CNames[i] = cursor.getString(1);
       cursor.moveToNext();

      }*/
    //String eventUriString = "content://com.android.calendar/events";
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(activity) + "events");
    ContentValues eventValues = new ContentValues();
    eventValues.put(Events.CALENDAR_ID,CalendarContract.Calendars._ID);

    eventValues.put(Events.TITLE, "Sale Near You");
    eventValues.put(Events.DESCRIPTION, "your sale time starting");
    eventValues.put(Events.EVENT_TIMEZONE,TimeZone.getDefault().getID());
    Log.i("DEBUG_TAG", "Timezone retrieved=>"+TimeZone.getDefault().getID());
    eventValues.put(Events.EVENT_TIMEZONE, "Delhi");
    eventValues.put(Events.DTSTART,startTime);
    eventValues.put(Events.DTEND, end1);
    eventValues.put("eventStatus", 1);
    eventValues.put("visibility", 3);
    eventValues.put("transparency", 0); 
    eventValues.put(Events.HAS_ALARM, true);
  //  Uri eventUri = con.getContentResolver().insert(Uri.parse(eventUriString), eventValues);
    Uri uri1 = cr.insert(Events.CONTENT_URI, eventValues);
    long eventID = Long.parseLong(uri1.getLastPathSegment());

    /***************** Event: Reminder(with alert) Adding reminder to event *******************/

    String reminderUriString = "content://com.android.calendar/reminders";

    ContentValues reminderValues = new ContentValues();

    reminderValues.put("event_id", eventID);
    reminderValues.put("minutes", 1);
    reminderValues.put("method", 1);

     cr.insert(Uri.parse(reminderUriString), reminderValues);
    Toast.makeText(activity, "Reminder have been saved succes fully", Toast.LENGTH_SHORT).show();
}  
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;
}



}

1 个答案:

答案 0 :(得分:4)

我得到了我的问题的解决方案,它在我的应用程序中完美运行...

  public class PushReminder {
Activity activity;
int sYear,sMonth,sDay,eYear,eMonth,eDay;

public PushReminder() {
    // TODO Auto-generated constructor stub
}

public PushReminder(String Start_date, String end_date,Activity ac) {
    // TODO Auto-generated constructor stub
    activity=ac;




   addReminder(Start_date,end_date);

}   



private void addReminder(String start, String end) {
    // TODO Auto-generated method stub
    int shour=17;
    int smin =30;
    int ehour=18;
    int emin =30;
    ContentResolver cr=activity.getContentResolver();

    Calendar beginTime=Calendar.getInstance();
    String[] sDat=start.split("-");
    String[] eDat=end.split("-");
     sDay=Integer.parseInt(sDat[0]);
    sMonth=Integer.parseInt(sDat[1]);
    sYear=Integer.parseInt(sDat[2]);

     eDay=Integer.parseInt(eDat[0]);
        eMonth=Integer.parseInt(eDat[1]);
        eYear=Integer.parseInt(eDat[2]);

    Log.i("msg","StartDay="+sDay+"StartMonth="+sMonth+"startYear"+sYear+"and sale end      date"+eDay+"Month"+eMonth+"Year="+eYear);    
    beginTime.set(sYear, sMonth-1, sDay,shour,smin);

    long startTime=beginTime.getTimeInMillis();
    Calendar endTime=Calendar.getInstance();

    endTime.set(eYear,eMonth-1,eDay,ehour,emin);
    long end1=endTime.getTimeInMillis();
     ContentValues calEvent = new ContentValues();
        calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick)
        calEvent.put(CalendarContract.Events.TITLE, "Add Shop Reminder");
        calEvent.put(CalendarContract.Events.DTSTART, startTime);
        calEvent.put(CalendarContract.Events.DTEND, end1);
        calEvent.put(CalendarContract.Events.HAS_ALARM, 1);
        calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE);
        Uri uri =cr.insert(CalendarContract.Events.CONTENT_URI, calEvent);

        // The returned Uri contains the content-retriever URI for 
        // the newly-inserted event, including its id
        int id = Integer.parseInt(uri.getLastPathSegment());
        Toast.makeText(activity, "Created Calendar Event " + id,
            Toast.LENGTH_SHORT).show();

       // String reminderUriString = "content://com.android.calendar/reminders";

        ContentValues reminders = new ContentValues();
        reminders.put(Reminders.EVENT_ID,id);
        reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
        reminders.put(Reminders.MINUTES, 10);

        Uri uri2 = cr.insert(Reminders.CONTENT_URI, reminders);



        Toast.makeText(activity, "Reminder have been saved succes fully", Toast.LENGTH_SHORT).show();
    }          // Toast.makeText(activity, "Reminder have been saved succes fully", Toast.LENGTH_SHORT).show();
    }