简体   繁体   中英

Help with intent starting on Android

I am trying to learn a few things from http://code.google.com/p/iosched/source/checkout . I wanted to see how they implemented that UI patterns they were talking about on the I/O.

In the HomeActivity they use this code to fire up the NotesActivity :

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

Notes class is in the ScheduleContract class and it looks like :

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);
    }
}

I cant see what exactly this code do, and how it ends up starting NotesActivity with notes loaded in. I also dont understand how and for what is URI used as a second parameter in new :
Intent(Intent.ACTION_VIEW, Notes.CONTENT_URI).
I searched on Google for explanation but failed to find simple one and simple examples. I would guess that Notes class is used to point to and format the data (notes) and then somehow NotesActivity is started, but dont understand exactly how.

In Android you never launch a specific application, at least not directly. What you do, is that you create an Intent , which is an abstract description of an operation to be performed :

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:

  • action -- The general action to be performed, such as ACTION_VIEW , ACTION_EDIT , ACTION_MAIN , etc.

  • data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri .

Whenever you want to launch another application, send a text message, pick a contact, activate the camera etc, you just create and start an Intent , and then Android figures out by itself what application it should launch.

So for your example with the Notes activity:

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

The first parameter, Intent.ACTION_VIEW , tells that this Intet will display something to the user. The second parameter, Notes.CONTENT_URI , is the Uniform Resource Identifier for the Notes activity (in your example, the URI can also include an ID if you want to open the activity with a specific note). The result is that the Notes activity is displayed for the user.

If you need more information, I suggest reading about Android Application Fundamentals and Intents and Intent Filters , which explain these concepts in detail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM