配置主Activity的方法:
在<activity>标签的内部加入<intent-filter>标签,并在标签里添加
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
这两句声明即可
那什么是<intent-filter>标签:
在Android开发中,<intent-filter>
标签用于定义应用组件(如Activity、Service或Broadcast Receiver)如何响应Intent。主要包含以下几个关键属性:
- Action:表示一个动作的字符串,系统通过这个属性来识别和执行相应的操作。例如,"android.intent.action.MAIN"代表应用的入口Activity。
- Category:用于进一步细化Action,它为Intent提供了额外的信息以帮助系统确定如何处理该Intent。比如,"android.intent.category.LAUNCHER"通常与"android.intent.action.MAIN"结合使用,表示该Activity是主入口并且会在启动器中显示出来。
- Data:指定了URI和数据的格式,用于过滤那些可以处理特定类型数据的Intent。比如,可以通过设置Data属性让某个Activity响应特定类型的URL或者特定路径的文件。
- Extras:提供额外的键值对数据,这些数据可以在发送Intent时附加给接收方使用。
此外,当Android系统收到一个Intent时,会根据Intent中的Action、Category等信息,以及在Manifest文件中声明的<intent-filter>
标签内容进行匹配,从而决定启动哪个应用组件。这个过程类似于一个过滤器,筛选出最合适的组件来处理特定的Intent。
示例代码修改:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AvtivityTest"
tools:targetApi="31">
<activity android:name=".FirstActivity"
android:label="This is the FirstActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>