Android日历:onActivityResult的resultCode始终为0 [英] Android Calendar: onActivityResult's resultCode is always 0

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

问题描述

我开发了一个Android应用程序,该应用程序提示日历应用程序编辑事件.

I develop an Android application that prompt the calendar application to edit events.

我使用startActivityForResult()打开日历.编辑并保存事件后,onActivityResult()中的resultCode始终为0.

I use startActivityForResult() to open the Calender. After editing and saving the event, resultCode is always 0 inside onActivityResult().

我看到了许多与"onActivityResult resultCode始终返回0"有关的答案. 这是因为在第二个活动中未使用setResult()finish().

I saw many answers related to "onActivityResult resultCode always returns 0". This is because of not using the setResult() and finish() in the 2nd activity.

但就我而言,我正在调用Android日历应用程序(不是自定义活动).

But in my case, I am calling the Android calendar application (not a custom activity).

提示Android日历的代码:

The code to prompt the Android calendar:

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
//set the event details
startActivityForResult(intent,1);

在保存或取消日历时触发

To trigger when calender is saved or cancelled

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //resultCode always returns 0.
    switch(requestCode) {
    case 1: 
        if (resultCode == Activity.RESULT_OK) 
        {

        }
    }
}

无论我在日历应用中单击保存"还是取消",resultCode始终为0.

Whether I click "Save" or "Cancel" in the calendar app, the resultCode always gives 0.

另外,我需要从日历意图中获取数据.但是onActivityResult中的意图数据"也返回null.

additionally I need to get the data back from the calendar intent.But The intent "data" in the onActivityResult also returns null.

任何人都可以解释为什么会发生吗?有什么办法知道用户单击保存"还是取消"?

Could anyone explain why it happens? Is there any way to know if user clicks "Save" or "Cancel"?

推荐答案

您可以检查新添加的日历事件的lastId,如果未更改,则结果实际上是CANCELLED,否则可以

You can check for lastId of newly added calendar event, if it has not changed, then result is actually CANCELLED, otherwise it is OK

val projection = arrayOf(CalendarContract.Calendars._ID)
cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, projection, null, null, null)
if (cursor.moveToLast()) {
    val lastId = cursor.getLong(0)
    // compare lastId with a previous one, if not changed - result is CANCELLED
}

这篇关于Android日历:onActivityResult的resultCode始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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