Android的日历,让事件ID [英] Android Calendar, get Event Id

查看:249
本文介绍了Android的日历,让事件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写需要一些事件添加到Android的日历应用程序。对于插入我只是用下面的code:

i'm writing an application that need to add some events to a calendar in android. For inserting i just used the following code:

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保存到我的应用程序,这样,用户可以直接从应用程序内召回该事件。 可能吗?

Now i'm wondering if is possible to get an id associated to that event, in that way after an event is inserted, and its id is saved into my application, the user can recall that event directly from inside the application. Is it possible?

推荐答案

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

I extracted a list of columns used to store events into android calendar. Here the list:

[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]exrule(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)

[0] "originalEvent" (id=830007842672)
[1] "availabilityStatus" (id=830007842752)
[2] "ownerAccount" (id=830007842840)
[3] "_sync_account_type" (id=830007842920)
[4] "visibility" (id=830007843008)
[5] "rrule" (id=830007843080)
[6] "lastDate" (id=830007843144)
[7] "hasAlarm" (id=830007843216)
[8] "guestsCanModify" (id=830007843288) [9] "guestsCanSeeGuests" (id=830007843376)
[10] "exrule" (id=830007843464)
[11] "rdate" (id=830007843528)
[12] "transparency" (id=830007843592)
[13] "timezone" (id=830007843672)
[14] "selected" (id=830007843744)
[15] "dtstart" (id=830007843816) [16] "title" (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] "description" (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为我的事件:

Then if i want to get the new event id for my event:

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;
}

和插入事件:

ANd for insert event:

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);
}

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

That solution seems to work, even if probably this is not a very good solution.

修改02/2015 getNextEventId的目的是创造一个新的事件条目的事件表,这里code相此方法的使用:

EDIT 02/2015 The purpose of getNextEventId is to create a new Event Entry for the event table, here the code with the usage of this method:

@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);
    }

更新09/2015

的要求,在评论我加入如何获得日历的URI(这基本上是哪里日历存储,应用程序尝试猜测它,搜索在所有已知的可能日历路径)

As requested in the comment i add how to get the Calendar URI (it is basically where the calendar is stored, and the application try to guess it, searching in all known possible calendar paths)

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆