编程语言
首页 > 编程语言> > 以编程方式从android中删除日历中的事件

以编程方式从android中删除日历中的事件

作者:互联网

我从互联网上搜索了很多,并尝试了很多例子.我可以通过我的应用程序成功地将事件添加到日历中,但我无法以编程方式删除此事件.以下是我尝试过的样本,但最终无法取得成功.

令牌[1]是事件ID.
1)

  Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
  Uri eventUri = ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
  getContentResolver().delete(eventUri, null, null); 

2)

 ContentResolver cr = FlightOperationsCancelTicketFee.this.getContentResolver();
 Uri EVENTS_URI =    Uri.parse("content://com.android.calendar/" + "events");
 deleteEvent(cr, EVENTS_URI, 1);


 private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) 
 {
     Cursor cursor;
     if (android.os.Build.VERSION.SDK_INT <= 7) 
     { 
         cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars_id=" + calendarId, null, null);
     } 
     else 
     { 
         cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null);
     }
     while(cursor.moveToNext()) 
     {
         long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
         resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
     }
     cursor.close();
 }

3)

 ContentResolver cr = getContentResolver();
 String calUriString = "content://com.android.calendar/events";
 Uri cal=Uri.parse(calUriString);
 String[] EVENT_PROJECTION=new String[]{"calendar_id","title","dtstart","_id"};


 Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
 Uri eventUri =ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
 String reminderUriString = "content://com.android.calendar/reminders";
 Uri remUri =Uri.parse(reminderUriString);
     cr.delete(remUri, "event_id="+Commons.event_id, null);
 cr.delete(eventUri, null, null);

4)

   Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
   Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, Long.parseLong(tokens[1]));
   getContentResolver().delete(eventUri, null, null);

以上都不起作用.我需要帮助 .谢谢 ..
编辑:我想我无法发送正确的上下文,有没有办法通过共享偏好保持上下文?但是,它只保留String和Int值.有没有其他方法可以做这样的事情?

解决方法:

使用此代码,它对我有用:

 Uri deleteUri = null;
        deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, Long.parseLong(String.valueOf(eventID)));
        int rows = getContentResolver().delete(deleteUri, null, null);
        Toast.makeText(this, "Event deleted", Toast.LENGTH_LONG).show();

标签:android,events,calendar,android-calendar,reminders
来源: https://codeday.me/bug/20190703/1369437.html