2014-01-09 94 views
0

我想添加事件的開始日期到結束日期,我已經做了很多谷歌, 但即時通訊面臨的錯誤,我也添加權限也在我的mainfest file.the問題是在我的代碼中執行的URI ...我寫了很多評論line..by我試圖通過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; 
} 



} 

回答

3

我得到了我的問題解,它完美的作品在我的應用程序...

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

我已經直接通過我的應用程序創建的日曆事件,並添加催還。他們正在完美地工作。但是當我點擊提醒通知時,它會打開設備日曆事件。現在我想定製提醒通知來加載我的應用程序,而無需加載默認日曆。我可以做到這一點嗎? –

+0

日曆ID如何?我在我的應用程序中創建了很多提醒,我如何決定ID? – Ishaan