Android进程间使用Intent进行通信
安卓使用Intent来封装程序的“调用意图”,使用Intent可以让程序看起来更规范,更易于维护。
除此之外,使用Intent还有一个好处:有些时候我们只是想要启动具有某种特征的组件,并不想和某个具体的组件耦合,使用Intent在这种情况下有利于解耦。
Action,Category属性与intent-filter配置
我们知道当需要进行Activity跳转的时候需要在manifests.xml文件中配置Activity信息。其中主Activity还需要配置<intent-filter>,并且在标签中还要配置<action>和<category>两个标签。
<?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.AcitvityTest" tools:targetApi="31"> <activity android:name=".lifecycle.SecondActivity"/> <activity android:name=".lifecycle.FirstActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> </application> </manifest>
其中Action代表该Intent所要完成的一个抽象“动作”,而category则用于为Action增加额外的附加类别信息。通常Action属性会与Category属性结合使用。
<action>和<category>两个标签中都可以指定android:name属性,该属性的值实际上就是字符串,<action>标签中的属性表明该Activity能够响应哪些Intent。
<intent-filer>标签实际上就是IntentFilet对象,用于声明该组件(比如Activity,Service,BroadcastReceiver)能够满足多少要求,每个组件可以声明自己满足多个Action要求,多个Category要求。只要某个组件能满足的要求大于等于Intent所指定的要求,那么该Intent就能启动该组件。
一个Intent对象只能包含一个Action属性,通过setAction(Stirng str)方法来进行设置,一个Intent对象可以包含多个Category属性,通过addCategory(String str)方法来进行添加。
当然,我们也可以通过设置Intent的Action和Category属性来跳转到系统的Activity
public class HomeActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); Button button = findViewById(R.id.home); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent); } }); } }
上述代码中配置的Action和Category对应的就是系统桌面,点击按钮后就会返回桌面。
到此这篇关于Android进程间使用Intent进行通信的文章就介绍到这了,更多相关Android Intent通信内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Kotlin FrameLayout与ViewPager2控件实现滚动广告栏方法
这篇文章主要介绍了Kotlin FrameLayout与ViewPager2控件实现滚动广告栏,FrameLayout与ViewPager2是Android开发中非常常见的布局组件,并且它不单单是一个帧布局组件,可以用它实现多种功能,感兴趣的朋友一起来看看吧2022-12-12Android中RecyclerView 滑动时图片加载的优化
本篇文章主要介绍了Android中RecyclerView 滑动时图片加载的优化,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04ActivityManagerService之Service启动过程解析
这篇文章主要为大家介绍了ActivityManagerService之Service启动过程解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-03-03准确测量 Android 应用中 Activity 和 Fragmen
在 Android 应用开发中,了解每个 Activity 和 Fragment 的启动时间对于性能优化至关重要,本文将介绍几种方法来准确测量 Activity 和 Fragment 的启动时间,并提供实际操作步骤,以帮助提升应用的响应速度和用户体验,需要的朋友可以参考下2024-07-07Android使用BottomTabBar实现底部导航页效果
这篇文章主要介绍了Android使用BottomTabBar实现底部导航页效果,本文通过实例代码结合文字说明的形式给大家介绍的非常详细,需要的朋友参考下吧2018-03-03
最新评论