2016-07-19 8 views
0

Ich verwende einen Code, um dem Kalender des Geräts ein Ereignis hinzuzufügen. Der Code funktioniert in allen Android-Versionen außer Android M. Das Ereignis, wenn es auf Android M hinzugefügt wird, wird als Geburtstag statt als Ereignis angezeigt. Bitte helfen Sie, wie dieses Problem behoben werden kann.Problem beim Hinzufügen eines Kalenderereignisses in Android M

Der Code verwendet wird, ist als

 // Add event to calendar 
     try { 
      ContentResolver cr = context.getContentResolver(); 
      ContentValues values = new ContentValues(); 
      values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds); 
      values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds); 
      values.put(CalendarContract.Events.TITLE, title); 
      values.put(CalendarContract.Events.DESCRIPTION, description); 
      if (location != null && (!TextUtils.isEmpty(location))) { 
       values.put(CalendarContract.Events.EVENT_LOCATION, location); 
      } 
      values.put(CalendarContract.Events.CALENDAR_ID, 1); 
      values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance() 
        .getTimeZone().getID());    
      Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 
      // Save the eventId into the Task object for possible future delete. 
      long eventId = Long.parseLong(uri.getLastPathSegment());    
      Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId); 
      Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri); 
      calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds); 
      calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds); 
      calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(calIntent);    

     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
+0

Versuch unter Methode –

Antwort

0

Versuch unten Methode @ Himmat

public void addReminder() { 
     Intent calIntent = new Intent(Intent.ACTION_EDIT); 
     calIntent.setData(CalendarContract.Events.CONTENT_URI); 
     calIntent.setType("vnd.android.cursor.item/event"); 
     calIntent.putExtra(CalendarContract.Events.TITLE, e.getName()); 
     calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation()); 
     calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription()); 
     Calendar calStart=Calendar.getInstance(); 
     if(Validator.isNotNull(e.getStartDate())) { 
      calStart.setTime(e.getStartDate()); 
     } 
     calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
       calStart.getTimeInMillis()); 
     Calendar calEnd=Calendar.getInstance(); 
     if(Validator.isNotNull(e.getEndDate())) { 
      calEnd.setTime(e.getEndDate()); 
     } 
     calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
       calEnd.getTimeInMillis()); 
     calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI, 
       e.getRegistrationLink()); 
     startActivity(calIntent); 
    } 
+0

Hallo folgt, wird diese Methode die Absicht für das Hinzufügen eines Ereignisses öffnen. Die Frage nach dem Hinzufügen eines Ereignisses progammatisch durch den Code selbst, ohne eine Absicht öffnen zu müssen. –

+0

@ Himmat siehe http://stackoverflow.com/questions/5976098/how-to-set-a-reminder-in-android sanjeev kumar die Antwort –

+0

Ich habe schon alle von ihnen Bruder versucht. Das Problem mit dem Code, den ich oben geschrieben habe, ist, dass für Android M-Geräte die calendarID keine ist. Daher wird kein Ereignis erstellt und stattdessen eine Geburtstagsbenachrichtigung erstellt. Also das Problem in meinem Code ist vor allem für Android M-Geräte –