【问题标题】:Help with intent starting on Android从 Android 开始的意图帮助
【发布时间】:2010-11-15 12:34:56
【问题描述】:

我正在尝试向http://code.google.com/p/iosched/source/checkout 学习一些东西。我想看看他们是如何实现他们在 I/O 上谈论的 UI 模式的。

HomeActivity 中,他们使用此代码来启动 NotesActivity

/* Launch list of notes user has taken*/
public void onNotesClick(View v) {  
    startActivity(new Intent(Intent.ACTION_VIEW, Notes.CONTENT_URI));
}

Notes 类在 ScheduleContract 类中,它看起来像:

public static class Notes implements NotesColumns, BaseColumns {
    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_NOTES).build();
    public static final Uri CONTENT_EXPORT_URI =
            CONTENT_URI.buildUpon().appendPath(PATH_EXPORT).build();

    /** {@link Sessions#SESSION_ID} that this note references. */
    public static final String SESSION_ID = "session_id";

    /** Default "ORDER BY" clause. */
    public static final String DEFAULT_SORT = NotesColumns.NOTE_TIME + " DESC";

    public static final String CONTENT_TYPE =
            "vnd.android.cursor.dir/vnd.iosched.note";
    public static final String CONTENT_ITEM_TYPE =
            "vnd.android.cursor.item/vnd.iosched.note";

    public static Uri buildNoteUri(long noteId) {
        return ContentUris.withAppendedId(CONTENT_URI, noteId);
    }

    public static long getNoteId(Uri uri) {
        return ContentUris.parseId(uri);
    }
}

我看不到这段代码到底做了什么,以及它如何最终启动 NotesActivity 并加载了笔记。我也不明白 URI 如何以及什么用作 new 中的第二个参数: 意图(Intent.ACTION_VIEW,Notes.CONTENT_URI)。
我在谷歌上搜索了解释,但没有找到一个简单的例子。我猜想 Notes 类用于指向和格式化数据(便笺),然后以某种方式启动 NotesActivity,但不明白具体是如何。

【问题讨论】:

    标签: android android-intent android-layout


    【解决方案1】:

    在 Android 中,您永远不会启动特定的应用程序,至少不会直接启动。你所做的是创建一个Intent,即an abstract description of an operation to be performed

    Intent 为 执行后期运行时绑定 不同代码之间 应用程序。它最重要的用途 正在开展活动, 它可以被认为是胶水 活动之间。它基本上是一个 被动数据结构持有 动作的抽象描述 被执行。主要部分 意图中的信息是:

    每当您想启动另一个应用程序、发送短信、选择联系人、激活相机等时,您只需创建并启动一个Intent,然后 Android 就会自行确定应该启动哪个应用程序。

    因此,对于您的 Notes 活动示例:

    startActivity(new Intent(Intent.ACTION_VIEW, Notes.CONTENT_URI));
    

    第一个参数Intent.ACTION_VIEW 告诉这个Intet 将向用户显示一些东西。第二个参数Notes.CONTENT_URI 是 Notes 活动的统一资源标识符(在您的示例中,如果您想打开带有特定注释的活动,URI 还可以包含一个 ID)。结果是为用户显示了 Notes 活动。

    如果您需要更多信息,我建议您阅读Android Application FundamentalsIntents and Intent Filters,它们详细解释了这些概念

    【讨论】:

    • 谢谢,我连接了 Intent 使用的基础知识。在我上面的示例中,他们使用的是内容提供程序,并且 URI 指向存储在该提供程序中的注释。
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多