gpt4 book ai didi

android - 我怎样才能找到我的日历 Intent 的结果?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:32 25 4
gpt4 key购买 nike

我在我的应用程序中启动日历的目的是:

    Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "Some title");
startActivity(intent);

如果用户继续并保存这个预先填充的日历条目,我不知道如何取回 eventID。我还想知道用户是否取消了日历提示,并且没有保存这个新的预填充事件。

是否有返回到 onActivityResult(...) 的相关信息,我可以将其用作日历事件的引用?我需要这个,以便稍后可以找到/打开日历事件以进行查看/编辑。 [更新:] 是的,尝试了 onActivityResult(...),只要日历在任何用户交互之前打开, Intent 就会返回,所以这是没有用的。

我想通过使用 Intent (也让用户从设备上可用的各种日历中进行选择)移交给日历应用程序来做到这一点,并避免从我的应用程序重新创建日历 UE。另外,我希望至少支持 Android 2.2+。

最佳答案

这是我的做法:

我在开始 Intent 之前获得下一个事件 ID:

public static long getNewEventId(ContentResolver cr) {      
Cursor cursor = cr.query(Events.CONTENT_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;
}

然后,我像往常一样调用 Intent :

        long event_id = EventUtility.getNewEventId(mContext.getContentResolver());

Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(Events._ID, event_id)
.putExtra(Events.TITLE, "title");

startActivity(intent);

这就是诀窍,在我的 Activity 的 onResume() 中,我检查是否创建了与之前生成的 event_id 相同的新 event_id。如果它们相同,则表示新日历已创建。然后,我将新的存储在我的数据库中。

public static long getLastEventId(ContentResolver cr) {      
Cursor cursor = cr.query(Events.CONTENT_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;
}

@Override
public void onResume() {
super.onResume();

long prev_id = EventUtility.getLastEventId(getContentResolver());

// if prev_id == mEventId, means there is new events created
// and we need to insert new events into local sqlite database.
if (prev_id == mEventID) {
// do database insert
}
}

关于android - 我怎样才能找到我的日历 Intent 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9761584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com