2016-07-05 4 views
1

デフォルトのカレンダーを開いてイベントを保存できます。カレンダーの[保存]ボタンをクリックするとメソッドが呼び出されます。私はこのコードを使用しています:カレンダーのボタンを処理するAndroid

public void InsertAnEvent() { 

    Calendar calendarEvent = Calendar.getInstance(); 
    Intent i = new Intent(Intent.ACTION_EDIT); 
    i.setData(CalendarContract.Events.CONTENT_URI); 
    i.putExtra("beginTime", calendarEvent.getTimeInMillis()); 
    i.putExtra("allDay", true); 
    i.putExtra("rule", "FREQ=YEARLY"); 
    i.putExtra("endTime", calendarEvent.getTimeInMillis() + 60 * 60 * 1000); 
    i.putExtra("title", "Eskuvo"); 

    activity.startActivityForResult(i, Activity.RESULT_CANCELED); 
} 

public void onActivityResult(int requestCode,int resultCode,Intent data){ 

    if(activity.RESULT_CANCELED==resultCode){ 
     Toast.makeText(activity, "No events added",Toast.LENGTH_LONG).show(); 
    } 
    else{ 
     getNewEventId(activity.getContentResolver(),CalendarContract.Events.CONTENT_URI,activity); 
    } 
} 

MainActivity.java

@Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){ 
    super.onActivityResult(requestCode,resultCode,data); 
    CalendarUtils u = new CalendarUtils(this); 
    u.onActivityResult(0,resultCode,data); 
} 

問題は、私が救う関係ないです

CalendarUtolis.javaをかどうか、それは常にその「いいえイベントを示していません追加されました。なぜあなたは考えていますか?

+0

どのように保存しますか? – Zoe

+0

[保存]ボタンをクリックして、イベントをカレンダーに保存します。 –

+0

http://stackoverflow.com/questions/9482514/android-calendar-get-event-id –

答えて

0

イベントをカレンダーに追加するには、次のコードを試してください。

最初にこのメソッドを呼び出します。

private void saveToCalender() 
{ 

    try { 

     long eventId=0; 
     try { 

      TGOption finalOption = options.get(0); 
      long startMili = finalOption.getTimeInterval(); 
      long endMili = finalOption.getToTimeInterval(); 

      eventId= addEventToCalender(this.getContentResolver(),this.project.getProjectName(),"","",1,startMili *1000,endMili*1000,true,false); 



     } 
     catch (Exception ex) 
     { 
      eventId=-1; 
     } 
     finally { 

      if(eventId>0) { 
      youract.this.finish(); 
      } 
      else 
      { 
       Toast.makeText(this,this.getResources().getString(R.string.calender_error),Toast.LENGTH_LONG).show(); 
      } 
     } 



    } 
    catch (Exception ex) 
    { 

    } 


} 

イベントメソッドを追加します。

/** 
* Add a new event into a native Google calendar. Add alert notification by setting <code>isRemind</code> as <code>true</code>. 
* @param cr - ContentResolver 
* @param title - Event title 
* @param addInfo - Event description 
* @param place - Event place 
* @param status - <code>int</code> This information is sufficient for most entries: tentative (0), confirmed (1) or canceled (2): 
* @param startDate - <code>long</code> event start time in mls 
* @param isRemind - <code>boolean</code> need to remind about event before? 
* @param isMailService - <code>boolean</code>. Adding attendees to the meeting 
* @return <code>long</code> eventID 
*/ 
public static long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status, 
             long startDate,long endDate, boolean isRemind, boolean isMailService) { 
    String eventUriStr = "content://com.android.calendar/events"; 
    ContentValues event = new ContentValues(); 
    // id, We need to choose from our mobile for primary its 1 
    event.put("calendar_id", 1); 
    event.put("title", title); 
    event.put("description", addInfo); 
    event.put("eventLocation", place); 
    event.put("eventTimezone", "UTC/GMT +2:00"); 

    // For next 1hr 

    event.put("dtstart", startDate); 
    event.put("dtend", endDate); 
    //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true 
    // values.put("allDay", 1); 
    event.put("eventStatus", 0); 
    event.put("hasAlarm", 1); 
    long eventID=0; 

    try { 

     Uri eventUri = cr.insert(Uri.parse(eventUriStr), event); 
     eventID = Long.parseLong(eventUri.getLastPathSegment()); 
    } 
    catch (Exception ex) 
    { 
     return -1; 
    } 



    try { 
     if (isRemind) { 
      String reminderUriString = "content://com.android.calendar/reminders"; 
      ContentValues reminderValues = new ContentValues(); 
      reminderValues.put("event_id", eventID); 
      // Default value of the system. Minutes is a integer 
      reminderValues.put("minutes", 5); 
      // Alert Methods: Default(0), Alert(1), Email(2), SMS(3) 
      reminderValues.put("method", 1); 
      cr.insert(Uri.parse(reminderUriString), reminderValues); //Uri reminderUri = 
     } 
    } 
    catch (Exception ex) 
    { 
     return eventID; 
    } 
    if (isMailService) { 
     String attendeuesesUriString = "content://com.android.calendar/attendees"; 
     /********* To add multiple attendees need to insert ContentValues multiple times ***********/ 
     ContentValues attendeesValues = new ContentValues(); 
     attendeesValues.put("event_id", eventID); 
     // Attendees name 
     attendeesValues.put("attendeeName", "xxxxx"); 
     // Attendee email 
     attendeesValues.put("attendeeEmail", "[email protected]"); 
     // Relationship_Attendee(1), Relationship_None(0), Organizer(2), Performer(3), Speaker(4) 
     attendeesValues.put("attendeeRelationship", 0); 
     // None(0), Optional(1), Required(2), Resource(3) 
     attendeesValues.put("attendeeType", 0); 
     // None(0), Accepted(1), Decline(2), Invited(3), Tentative(4) 
     attendeesValues.put("attendeeStatus", 0); 
     cr.insert(Uri.parse(attendeuesesUriString), attendeesValues); //Uri attendeuesesUri = 
    } 

    return eventID; 
} 
+0

このコードを使用すると、自分でレイアウトを作成する必要があります。 –

+0

はい、独自のレイアウトをクリックしてこのメ​​ソッドを呼び出すことができます。 – Roadies

+0

しかしこれがインテントを使いたい理由です。それは完璧なイベントクリエイターなので、私がする必要があるのはイベントのIDを保存することだけです。だから私はセービングを処理する必要があります。 –

関連する問題