Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何按应用程序id而不是按事件id数组从设备日历中批量删除事件_Android_Android Calendar - Fatal编程技术网

Android 如何按应用程序id而不是按事件id数组从设备日历中批量删除事件

Android 如何按应用程序id而不是按事件id数组从设备日历中批量删除事件,android,android-calendar,Android,Android Calendar,以下是我在应用程序中创建日历事件的方式: for(CalendarEventDescriptor calendarEventDescriptor : calendarEventDescriptors.values()) { if(calendarEventDescriptor.startMilliseconds>now){ values = new ContentValues();

以下是我在应用程序中创建日历事件的方式:

for(CalendarEventDescriptor calendarEventDescriptor : calendarEventDescriptors.values()) {
                if(calendarEventDescriptor.startMilliseconds>now){

                    values = new ContentValues();
                    values.put(CalendarContract.Events.DTSTART, calendarEventDescriptor.startMilliseconds);
                    values.put(CalendarContract.Events.DTEND, calendarEventDescriptor.endMilliseconds);
                    values.put(CalendarContract.Events.TITLE, calendarEventDescriptor.title);
                    values.put(CalendarContract.Events.DESCRIPTION, calendarEventDescriptor.description);

                    values.put(CalendarContract.Events.CALENDAR_ID, 1);
                    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
                    uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
                    calendarEventDescriptor.eventId = Long.parseLong(uri.getLastPathSegment());
                }
            }
在写这篇文章的时候,我存储了我创建的所有事件ID的数组,这样当用户轻按开关时,我就可以遍历它们并从日历中删除它们

for(long eventId : eventIds) {
                if(eventId>0){
                    Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
                    rowsDeleted += application.getContentResolver().delete(deleteUri, null, null);
                }
            }
我突然想到,可以为其中一个CalendarContract.Events设置自定义值。列,这样我可以一次删除所有事件,并且我不必记住它们的ID,我总是将它们全部删除,从不删除某些事件


这是可能的,哪一个日历是contract.Events。我应该使用列以及如何删除?这是一个很好的问题!我同意,在这种情况下,ContentValues中的额外属性才是解决方法。 我已经通过重新使用CalendarContract.Events.CUSTOM_APP_软件包完成了这项工作,该软件包对于用户来说是不可见的,到目前为止,没有发现任何副作用:

因此,我正在创建事件,就像您所做的那样:

        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.CALENDAR_ID, 1);
        values.put(CalendarContract.Events.DTSTART, ...);
        values.put(CalendarContract.Events.DTEND, ...);
        values.put(CalendarContract.Events.TITLE, ...);
        values.put(CalendarContract.Events.DESCRIPTION, ....);
        values.put(CalendarContract.Events.CUSTOM_APP_PACKAGE, getApplicationContext().getPackageName());
        values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
        cr.insert(CalendarContract.Events.CONTENT_URI, values);
一旦我需要删除所有这些内容,我会调用:

    Cursor cursor = cr
            .query(CalendarContract.Events.CONTENT_URI,
                   new String[] { CalendarContract.Events._ID, CalendarContract.Events.CUSTOM_APP_PACKAGE },
                   null, null, null);
    cursor.moveToFirst();

    String idsToDelete = "";
    for (int i = 0; i < cursor.getCount(); i++) {
        // it might be also smart to check CALENDAR_ID here
        if (getApplicationContext().getPackageName().equals(cursor.getString(1))) {
            idsToDelete += String.format("_ID = %s OR ", cursor.getString(0));
        }

        cursor.moveToNext();
    }

    if (idsToDelete.endsWith(" OR ")) {
        idsToDelete = idsToDelete.substring(0, idsToDelete.length()-4);
    }

    cr.delete(CalendarContract.Events.CONTENT_URI, idsToDelete, null);
我希望它能帮助您在一个事务中使用来执行多个删除/插入


检查代码示例。

经过长时间的研究,我得到了一种新的谷歌日历文档 您可以将多个参数添加到一个选择中,以便查找所有所需的行并一次将它们全部删除

val operationList = ArrayList<ContentProviderOperation>()
var contentProviderOperation: ContentProviderOperation

appointments.forEach {
   contentProviderOperation = ContentProviderOperation
  .newDelete(CalendarContract.Events.CONTENT_URI)
  .withSelection(CalendarContract.Events.ORIGINAL_SYNC_ID + " =?", arrayOf(it.id))
  .build()

  operationList.add(contentProviderOperation)
}

contentResolver.applyBatch(CalendarContract.AUTHORITY, operationList)

因此,如果不使用循环,就无法实际删除事件,只需说“嘿,一次删除与此应用程序包对应的所有事件?”?我真的很想防止循环,因为删除300个事件(例如)需要永远的时间,好的一面是我不必将事件ID保存在应用程序存储中,我可以随时从卸载的应用程序中删除以前的事件,对吗?@J.K.是的,一旦事件设置了属性,你可以随时删除它,事实上,即使是从不同的应用程序。我会考虑是否有可能把它装得更大,够得着out@J.K.我已经更新了代码,现在它没有用于删除的循环。过来看!多谢各位!我将在20小时内颁发赏金