Android文档Declaring the activity in the manifest

March 14, 2012


Declaring the activity in the manifest


You must declare your activity in the manifest file in order for it to be accessible(容易进入的) to the system. To decalare your activity, open your manifest file and add an <activity> element as a child of the <application> element.


<manifest ... > 
<application ... >
<activity android:name=".ExampleActivity" /> 
</application ... > 
</manifest >


There are several other attributes(属性) that you can include in this element, to define properties(属性) such as the label for the activity, an icon for the activity, or a theme(主题) to style(样式) the activity's UI. See the <activity> element reference(参考) for more information about available(可用的) attributes(属性).

Using intent filters 使用intent过滤器


An <activity> element can also specify(列举) various(不同的) intent filters—using the <intent-filter> element—in order to declare how other application components(部件) may activate(激活) it.
binkery:当你使用Android SDK创建一个应用的时候,自动生成的activity的子类已经包含了一个intent-filter。

When you create a new application using the Android SDK tools, the stub activity that's created for you automatically(自动的) includes an intent filter that declares the activity responds to the "main" action and should be placed in the "launcher" category(分类). The intent filter looks like this:

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />


The <action> element specifies(指定) that this is the "main" entry(入口) point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).

If you intend(打算) for your application to be self-contained(独立的) and not allow other applications to activate its activities, then you don't need any other intent filters. Only one activity should have the "main" action and "launcher" category, as in the previous(之前的) example. Activities that you don't want to make available(可用的) to other applications should have no intent filters and you can start them yourself using explicit(明确的) intents (as discussed(讨论) in the following section).

However, if you want your activity to respond to implicit(隐式的) intents that are delivered(传递) from other applications (and your own), then you must define additional(附件的) intent filters for your activity. For each type of intent to which you want to respond, you must include an <intent-filter> that includes an element and, optionally(可选的), a element and/or a element. These elements specify(指定) the type of intent to which your activity can respond.

For more information about how your activities can respond to intents, see the Intents and Intent Filters document.

--- EOF ---