0

我想在我的应用程序中单击按钮打开 android 本机日历应用程序。我在网上搜索了一下,发现代码如下:

Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", eventStartInMillis);
intent.putExtra("endTime", eventEndInMillis);

startActivity(intent);

有人可以向我解释这段代码吗?

4

1 回答 1

0

回答关于代码做什么的问题

Intent intent = new Intent(Intent.ACTION_EDIT);  

创建一个与ActionIntent对应的新对象。这是一个通用的 Intent Action,这意味着仅此还不足以自动启动权利。这就是为什么我们接下来要做的是设置. 由于我们要启动日历编辑器,我们将类型设置为:IntentACTION_EDITActivityIntentvnd.android.cursor.item/event

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

这与清单条目相对应。注意data节点(显示了 Jellybean 版本清单)。

<intent-filter>
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.INSERT" />
    <category android:name="android.intent.category.DEFAULT" />
    <!-- mime type -->
    <data android:mimeType="vnd.android.cursor.item/event" /> 
</intent-filter>

这些putExtra()调用允许您指定事件特征,例如事件的标题、开始和结束时间等。

然后我们启动Activity。结果是这样的:

在此处输入图像描述

然后,您已经打开了 Android 日历应用程序来添加事件!

警告:里程可能会有所不同

但是,如果您不想调用事件编辑器怎么办?如果您只想打开 Android 日历应用程序,则可以使用PackageManager#getLaunchIntentForPackage()(前提是:包名称未更改,已安装 Android 日历,并且您可以获得对 的引用PackageManager)。

来自 Activity 的示例(使用 MainActivity.this 突出显示我从 Activity 启动的事实):

Intent i = MainActivity.this.getPackageManager().getLaunchIntentForPackage("com.android.calendar");
if (i != null)
  startActivity(i);
于 2013-03-17T00:05:56.543 回答