Android日历,获取事件ID

我正在编写一个需要向Android日历中添加一些事件的应用程序。对于插入,我只使用了以下代码:

public void onItemClick(AdapterView<?> adapter, View curview, int position, long id) {

WhoisEntry entry = this.adapter.getItem(position);

String domainName = entry.getDomainName();

Date expDate = entry.expirationDate;

Toast.makeText(getApplicationContext(), "Domain: " + domainName, Toast.LENGTH_SHORT).show();

Calendar cal = Calendar.getInstance();

Intent intent = new Intent(Intent.ACTION_EDIT);

intent.setType("vnd.android.cursor.item/event");

intent.putExtra("beginTime", entry.expirationDate);

intent.putExtra("allDay", false);

intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);

intent.putExtra("title", "Expiration of " + entry.domainName);

startActivity(intent);

}

现在,我想知道是否有可能获得与该事件关联的ID,以这种方式在插入事件并将其ID保存到我的应用程序后,用户可以直接从应用程序内部调用该事件。可能吗?

回答:

我提取了用于将事件存储到android日历中的列的列表。这里的清单:

[0]“ originalEvent”(id = 830007842672)

[1]“ availabilityStatus”(id = 830007842752)

[2]“ ownerAccount”(id = 830007842840)

[3]“ _sync_account_type”(id = 830007842920)

[4]“可见性” (id = 830007843008)

[5]“ rrule”(id = 830007843080)

[6]“ lastDate”(id = 830007843144)

[7]“ hasAlarm”(id = 830007843216)

[8]“ guestsCanModify”(id = 830007843288)[ 9]“ guestsCanSeeGuests”(id =

830007843376)

[10]“执行”(id = 830007843464)

[11]“ rdate”(id = 830007843528)

[12]“透明度”(id = 830007843592)

[13]“时区”( id = 830007843672)

[14]“已选择”(id = 830007843744)

[15]“ dtstart”(id = 830007843816)[16]“标题”(id = 830007843888)

[17]“ _sync_time”(id = 830007843952)

[18]“ _id”(id = 830007844024)[19]“ hasAttendeeData”(id = 830007844088)[20]“

_sync_id”(id = 830007844176)

[21]“ commentsUri” (id = 830007844248)[22]“说明”(id = 830007844328)[23]“

htmlUri”(id = 830007844408)[24]“ _sync_account”(id = 830007844480)

[25]“ _sync_version”(id = 830007844560)

[ 26]“ hasExtendedProperties”(id = 830007844640)

[27]“ calendar_id”(id = 830007844736)

然后,如果我想获取事件的新事件ID:

public static long getNewEventId(ContentResolver cr, Uri cal_uri){      

Uri local_uri = cal_uri;

if(cal_uri == null){

local_uri = Uri.parse(calendar_uri+"events");

}

Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");

cursor.moveToFirst();

long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));

return max_val+1;

}

和插入事件:

public void insertDomainEntry(Date exp_date, String name, long event_id){

SQLiteDatabase db = getWritableDatabase();

ContentValues values = new ContentValues();

values.put("exp_date", exp_date.getTime()/1000);

values.put("event_id", event_id);

values.put("domainname", name);

db.insertOrThrow("domains_events", null, values);

}

该解决方案似乎可行,即使这可能不是一个很好的解决方案。

的目的是为事件表创建一个新的事件条目,此处是使用此方法的代码:

@Override

public void onItemClick(AdapterView<?> adapter, View curview, int position,

long id) {

WhoisEntry entry = this.adapter.getItem(position);

long event_id = CalendarUtils.getNewEventId(getContentResolver(), null);

Toast.makeText(getApplicationContext(), "Domain: " + entry.getDomainName(),

Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Intent.ACTION_EDIT);

intent.setType("vnd.android.cursor.item/event");

intent.putExtra("beginTime", entry.getExpiration().getTime());

intent.putExtra("_id", event_id);

intent.putExtra("allDay", false);

intent.putExtra("endTime", entry.getExpiration().getTime()+60*30);

intent.putExtra("title", "Expiration of " + entry.getDomainName());

startActivity(intent);

database.insertDomainEntry(entry.getExpiration(),

entry.getDomainName(), event_id);

}

根据评论中的要求,我添加了如何获取Calendar URI(基本上是日历的存储位置,应用程序尝试猜测它,在所有已知的可能的日历路径中进行搜索)

public static String getCalendarUriBase(Activity act) {     

String calendarUriBase = null;

Uri calendars = Uri.parse("content://calendar/calendars");

Cursor managedCursor = null;

try {

managedCursor = act.getContentResolver().query(calendars,

null, null, null, null);

} catch (Exception e) {

}

if (managedCursor != null) {

calendarUriBase = "content://calendar/";

} else {

calendars = Uri.parse("content://com.android.calendar/calendars");

try {

managedCursor = act.getContentResolver().query(calendars,

null, null, null, null);

} catch (Exception e) {

}

if (managedCursor != null) {

calendarUriBase = "content://com.android.calendar/";

}

}

calendar_uri= calendarUriBase;

return calendarUriBase;

}

以上是 Android日历,获取事件ID 的全部内容, 来源链接: utcz.com/qa/400648.html

回到顶部