在我的应用中,我在屏幕上的Google日历中添加了一些事件。当用户再次进入屏幕时,它会再次添加事件,以便在日历中重复事件也可见。 因此,在添加任何新事件之前,我想检查事件是否已经存在。如果我有日历的所有事件和遍历它,我的应用程序变得很慢或通知挂起获取我的应用在Android日历中添加的事件
too many calendar deletes
,我使用插入值的代码是:
final ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calendarId);
event.put(CalendarContract.Events.TITLE, name);
event.put(CalendarContract.Events.DTSTART, millisstarttime);
event.put(CalendarContract.Events.DTEND, millisendtime);
event.put(CalendarContract.Events.EVENT_LOCATION, venue);
event.put(CalendarContract.Events.ALL_DAY, 0); // 0 for false, 1 for true
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_PRIVATE);
event.put(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);// 0 for false, 1 for true
String timeZone = TimeZone.getDefault().getID();
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
this.getContentResolver().insert(baseUri, event);
我想获得的值:
private void deleteeve(ContentResolver cr,int calendarId) {
String[] projection = {CalendarContract.Events._ID,
CalendarContract.Events.CUSTOM_APP_PACKAGE,CalendarContract.Events.CALENDAR_ID,CalendarContract.Events.ORGANIZER};
// Get a Cursor over the Events Provider.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
Cursor cursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, projection,"calendar_id=" + calendarId, null,
null);
assert cursor != null;
cursor.moveToFirst();
String idsToDelete = "";
int nameIdx = cursor.getColumnIndexOrThrow(CalendarContract.Events.CUSTOM_APP_PACKAGE);
int id=cursor.getColumnIndexOrThrow(CalendarContract.Events._ID);
int org=cursor.getColumnIndexOrThrow(CalendarContract.Events.ORGANIZER);
while (cursor.moveToNext()) {
// Extract the name.
String name = cursor.getString(nameIdx);
String apppackage = getApplicationContext().getPackageName();
String organizer=cursor.getString(org);
String idevent = cursor.getString(id);
for (int i = 0; i < cursor.getCount(); i++) {
if (apppackage.equals(name)) {
// MY CODE
}
}
}
}
我试图让使用包名的事件,所以,反正是有得到的事件仅我的应用程序添加,比遍历所有的其他事件?
您使用哪个calendar_id? –
@JohnKalimeris我的一个Google帐户的日历ID。 –
您可以为您的应用程序创建新的本地日历并存储您的活动。然后你只能查询这个日历中的所有事件。如果您愿意,我可以为您提供一些帮助代码。 –