记事本问题:getIntent()和设置数据(通过引用?)

时间:2011-12-28 10:22:11

标签: android android-intent

所以我试图理解NotePad示例中的所有代码,但是我遇到了一些困难:

首先,如何判断我们的项目是从“X”文件开始的?像一个“索引”文件..?

(我猜它是从NoteList页面开始的)在这个NoteList页面中,我们使用getIntent()获取当前意图,然后获取其中的数据:但是:

  • 哪个活动开始了笔记清单? (我认为没有,并且这个项目中的所有活动都有这个getIntent()方法,所以当项目启动时是否会自动“发送”?)
  • 我们将数据设置为新的intent变量,但是我们然后像这样在游标中提取数据:getIntent().getData():它是通过“reference”传递还是类似的?那么使用新的intent变量或getIntent()是一样的吗?

感谢您的帮助; - )

>

// Gets the intent that started this Activity.
Intent intent = getIntent();

// If there is no data associated with the Intent, sets the data to the default URI, which
// accesses a list of notes.
if (intent.getData() == null) {
    intent.setData(NotePad.Notes.CONTENT_URI);
}

/*
 * Sets the callback for context menu activation for the ListView. The listener is set
 * to be this Activity. The effect is that context menus are enabled for items in the
 * ListView, and the context menu is handled by a method in NotesList.
 */
getListView().setOnCreateContextMenuListener(this);

/* Performs a managed query. The Activity handles closing and requerying the cursor
 * when needed.
 *
 * Please see the introductory note about performing provider operations on the UI thread.
 */
Cursor cursor = managedQuery(
    getIntent().getData(),            // Use the default content URI for the provider.
    PROJECTION,                       // Return the note ID and title for each note.
    null,                             // No where clause, return all records.
    null,                             // No where clause, therefore no where column values.
    NotePad.Notes.DEFAULT_SORT_ORDER  // Use the default sort order.
);

1 个答案:

答案 0 :(得分:2)

NoteList是App的主要入口点。请参阅AndroidManifest.xml以查看Launcher和主要意图过滤器

 <activity android:name="NotesList" android:label="@string/title_notes_list">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
        </intent-filter>
    </activity>

每个活动都由Intent启动。在Activity中调用getIntent()。此调用返回启动它的Intent对象。在记事本中,NoteID - (这是一个URI)从一个活动传递给Intent对象中的参数。