For android.intent.action.MAIN understanding and android.intent.category.LAUNCHER

When we use Android Studio to create a project and generate an Activity, the following code can often be seen in the manifest file:

        <Activity Android: name =. "the MainActivity">
            <filter-Intent>
                <Action Android: name = "android.intent.action.MAIN" />
                <category Android: name = "android.intent.category.LAUNCHER" />
            </ the Intent-filter>
        </ Activity>

If we create a second Activity, then the label which is silent and no action category, then here android.intent.action.MAIN What is the role and android.intent.category.LAUNCHER it?

By API documentation, we understand that their role is as follows:

android.intent.action.MAIN: Entrance Activity decide the application, which is displayed first when we start the Activity application. 
android.intent.category.LAUNCHER: represents the activity should be included in the system launcher (launcher) (allowing the user to start it). Android Launcher is the desktop launcher, is collectively referred to as the desktop UI.

Are part of the action and category of Intent, the former said the intent of the action, which represents the intention of categories. So for the above paragraph, I understand that: android.intent.action.MAIN show the first start of the Activity application when a start-up operation. What started it? android.intent.category.LAUNCHER tell us what action is: through the App launcher (Personally, I understood as a desktop icon) to start.

Preparatory work
we can do some "damage" to deepen understanding. Prior to this point the preparatory work to do first: to create a simple project (the name of my application in android: label modified to "My Apps"), which will have a default MainActivity, it is the entrance Activity. Then we'll create a SecondActivity, the compiler will automatically help us up for good in the manifest file. The main contents of the manifest file as follows:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <Activity Android: name =. "SecondActivity"> </ Activity>
    </ file application>

To facilitate the identification of these two Activity, we gave a TextView they are added in the layout, the text is their name:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lin.mr.activityheima.MainActivity">
    <TextView
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Android: text = "the MainActivity" />
</ the RelativeLayout>

SecondActivity layout Similarly, there is not posted.

Well, let's start the sabotage.

A destruction: Activity remove all the android.intent.action.MAIN
        <Android Activity: name = "the MainActivity.">
            <Filter-Intent>
                <category Android: name = "android.intent.category.LAUNCHER" />
            </ Intent -filter>
        </ Activity>
        <Activity Android: name =. "SecondActivity"> </ Activity>

If we remove all Activity android.intent.action.MAIN happen? Very simple, because the entrance Activity android.intent.action.MAIN decided, after removing the application it can not find the entrance, do not know which one Activity to start. If the Activity likened to face Android applications, Android applications on it at this time face to see people. 
Select the drop-down menu next to the Run button will prompt the current application error: 


If forced to run, then it will fail to start: 


But there is a special place, open the phone settings, click on "Application Management", find it? Installed applications is the "My application". That it is able to successfully install, but due to the lack of entry, so the desktop can not see it.

Destroyed two: remove all android.intent.category.LAUNCHER Activity of
the destruction of a known, we must have an entry Activity, so we android.intent.action.MAIN back MainActivity. Then we just get rid of MainActivity in android.intent.category.LAUNCHER.

        <Activity Android: name =. "the MainActivity">
            <filter-Intent>
                <Action Android: name = "android.intent.action.MAIN" />
            </ Intent-filter>
        </ Activity>
        <Android Activity: name = " .SecondActivity "> </ activity>

said before, android.intent.category.LAUNCHER Activity decide whether the inclusion of the starter system, so at this time we do not have an Activity has this code, which means that none of Activity in the startup list in. I believe you have guessed the results, the compiler will directly prompt wrong, just like the screenshot as a destruction of. At this point, open the phone settings "application management", you can see the application successfully installed, but because there is no App launcher, it can not be found on the desktop.

Destroyed three: more Activity has android.intent.action.MAIN
can see from the damage and destruction of a Second, write an application is running requires at least one Activity also has android.intent.action.MAIN and android.intent.category .LAUNCHER of. We add them in MainActivity, but we are also SecondActivity added android.intent.action.MAIN:

        <Activity Android: name =. "the MainActivity">
            <filter-Intent>
                <Action Android: name = "android.intent.action.MAIN" />
                <category Android: name = "android.intent.category.LAUNCHER" />
            </ Intent-filter>
        </ Activity>
        <Android Activity: name =. "SecondActivity">
            <filter-Intent>
                <Action Android: name = "android.intent.action.MAIN" />
            </ filter-Intent>
        < / activity>

what happens then? After the run we can see the desktop to generate a "My Apps" icon, this application is only one Activity, is MainActivity. Visible SecondActivity did not show his face. It is also well understood, it is missing android.intent.category.

Destroyed four: more Activity also has android.intent.action.MAIN and android.intent.category.LAUNCHER
us to SecondActivity also add android.intent.category.LAUNCHER, so almost no difference between the two Activity. At the same time, we give SecondActivity plus one: android: label = "My Application 2", you will know the reason behind.

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name = "android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"
            android:label="我的应用2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name = "android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

We already know that learning through the front, which is quite set up two entrance ah, the Android application will face the forks in the road how to do it? We destroy Three generated "My Applications" to uninstall, and then run the App. This time the magic happened! On the desktop simultaneously generate two icons, one is "My Own" and the other is "My application 2."

Click on "My application", we can see that MainActivity, ah, just like before. Then the "My Apps 2" mean? Click on it and you'll see SecondActivity.

Thus we can know, when an Android application Activity more than one entry, the installation App will generate a corresponding number of icons (you can use android: icon set different icon). As for the name of the icon, if you do not have a set per Activity, then the application will use the name, such as MainActivity that's it. But beware, these icons represent are the same application! Do not believe it, you open the "Application Management" in your phone settings, in the "Installation" you can see that there is a "my applications" and not "My application 2."

In addition, you can also engage in a final destruction verify this: by dragging icons uninstall the application, regardless of which icon is dragged, and finally they all disappear together.

Well, so do the damage, I have my own understanding of android.intent.action.MAIN and android.intent.category.LAUNCHER, I do not know how you understand it?
----------------
Disclaimer: This article is the original article CSDN bloggers "Lindroy", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/lindroid20/article/details/51993247

发布了36 篇原创文章 · 获赞 162 · 访问量 62万+

Guess you like

Origin blog.csdn.net/qq_19004627/article/details/103911734