【Android開發那點破事】Android調試安裝出現2個或2個以上圖標

前些日子給我們的APP添加了啓動畫面功能,這個在前面已經說過了: 【Android開發那點破事】打開APP加載頁面實現

今天在用真機調試的時候,發現app安裝到手機上出現了2個圖標。

這裏先將解決方法貼出來,後面再簡單分析下爲什麼會出現2個圖標。

解決方案:

在AndroidManifest.xml, CTRL+F中找到所有包含:

category android:name="android.intent.category.LAUNCHER"

把多餘的刪除,只保留你APP啓動時的第一個activity中的。比如我這裏只保留APP加載頁面的配置:

<!-- startup page -->
<activity android:name="com.withiter.quhao.activity.LaunchActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<!-- startup page -->

我們來分析下爲什麼會這樣:

首先看下android官方的一些描述:

{ action=android.app.action.MAIN } matches all of the activities that can be used as top-level entry points into an application.

{ action=android.app.action.MAIN, category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.

{ action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes } displays a list of all the notes under "content://com.google.provider.NotePad/notes", which the user can browse through and see the details on.

{ action=android.app.action.PICK data=content://com.google.provider.NotePad/notes } provides a list of the notes under "content://com.google.provider.NotePad/notes", from which the user can pick a note whose data URL is returned back to the caller.

{ action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note } is similar to the pick action, but allows the caller to specify the kind of data they want back so that the system can find the appropriate activity to pick something of that data type.

{ action=android.app.action.MAIN, category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.這個翻譯過來就是添加了{ action=android.app.action.MAIN, category=android.app.category.LAUNCHER } 的activity 是在程序啓動的列表中的。所以如果定義多個LAUNCHER,就會出現多個APP圖標。


最後我們來總結下:
一個應用程序可以有多個Activity,每個Activity是同級別的,那麼在啓動程序時,最先啓動哪個Activity呢?有些程序可能需要顯示在 程序列表裏,有些不需要。怎麼定義呢?android.intent.action.MAIN決定應用程序最先啓動的Activity ,是程序的入口android.intent.category.LAUNCHER決定應用程序是否顯示在程序列表裏。因爲你的程序可能有很多個activity只要xml配置文件中有這麼一個intent-filter,而且裏面有這個launcher,那麼這個activity就是點擊程序時最先運行的那個activity。現在你只有一個activity,那麼加不加就沒有關係了。用於模擬器啓動時設置爲默認打開爲的activity。


好了今天這個破事就到這裏,其實android開發就這麼點破事。關於其他破事,見專欄:

更多Android開發的破事,請看專欄:《Android開發那點破事》


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章