2010-11-15 77 views
0

我想从http://code.google.com/p/iosched/source/checkout了解一些事情。我想看看他们是如何实现他们在I/O上讨论的UI模式的。意向开始于Android的帮助

HomeActivity他们使用此代码来火了NotesActivity

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

票据类是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并加载了笔记。我也不知道d是什么URI用作新的第二个参数:
意图(Intent.ACTION_VIEW,Notes.CONTENT_URI)。
我在Google上搜索了解释,但没有找到简单的例子。我猜Notes类是用来指向和格式化数据(注释),然后以某种方式NotesActivity开始,但不明白如何。

回答

0

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

意图提供了一个工具用于 进行后期运行在不同的 应用程序的代码之间的结合 。其最重要的用途 正在开展活动, 它可以被认为是活动之间的胶水 。它基本上是一个 被动数据结构,其中包含一个 抽象描述的动作,执行到 。的 信息中的意图初级件是:

  • 动作 - 被执行以 一般操作,如 ACTION_VIEWACTION_EDITACTION_MAIN

  • 数据 - 要操作的数据, ,例如 联系人数据库中的人员记录,表示为 Uri

每当你想启动其他应用程序,发送短信,选择联系人,启动相机等,你只需要创建和启动Intent,然后安卓本身断定哪些应用它应该发射。

所以对于与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,这说明这些概念详细

+0

谢谢,我使用连接意向的基础知识。在我上面的例子中,他们使用内容提供者,URI指向存储在该提供者中的注释。 – 2010-11-15 13:44:56