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

问题描述

我正在尝试将事件的开始日期添加到结束日期,我做了很多Google搜索工作, 但是我面临错误,我也对我的mainfest文件具有添加权限.问题出在执行URI的代码中...我写了很多注释行..据此,我试图通过Uri,但我仍然收到error.ie读取异常并插入查询中..请帮助我.预先感谢

I am trying to add the event for start date to end date,I have done lots of googleing, but i m facing error,i have add permissions also in my mainfest file.the problem is in my code in executing the URI ...i have written lots of comment line..by that i hav tried to pass Uri,but still i am getting error.i.e read exception and in inserting the query..please help me.Thanks in advance

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



}

推荐答案

我已经解决了我的问题,它在我的应用程序中非常有效...

I got solution of my question, it works perfectly in my app...

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

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

08-13 21:02