在我的应用中,我在屏幕上的Google日历中添加了一些事件。当用户再次进入屏幕时,它会再次添加事件,因此在日历中也可以看到重复事件。 所以在添加任何新事件之前,我想检查事件是否已经存在。如果我收到日历中的所有事件并对其进行迭代,我的应用程序会变得很慢或挂起通知
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
}
}
}
}
我尝试使用包名来获取事件,那么无论如何只能通过我的应用程序添加事件,而不是迭代所有事件?
答案 0 :(得分:0)
如果您不想为自己的应用创建新的日历,可以试试这个。
Cursor cursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, projection, CalendarContract.Events.CUSTOM_APP_PACKAGE = getApplicationContext().getPackageName(), null,
null);
答案 1 :(得分:0)
Thread mThread = new Thread(new Runnable(){
@override
public void run(){
//MY stuff
}
});
这就是全部。