Intent(信使)显式与隐式调用
来源:互联网 发布:天猫淘宝京东的区别 编辑:程序博客网 时间:2024/06/01 09:57
Intent(信使)显式与隐式调用
程序的3个核心组件——Activity、Service、BroadcastReceiver——是通过Intent传递消息的,本篇简单列举一下Intent在Activity间的显式和隐式的使用方法。
显式Intent的使用
显式,顾名思义,就是明显地写出将要跳转的代码文件的名字。
标准版
Intent intent = new Intent(); intent.setClass(MainActivity.this,SecondActivity.class); startActivity(intent);
千万别忘了在AndroidManifest文件中注册Activity
<activity android:name=".SecondActivity"> </activity>
两行代码
Intent intent = new Intent(this,SecondActivity.class);startActivity(intent);
丧心病狂型
startActivity(new Intent(this,SecondActivity.class));
这里建议使用标准版,清晰明了,便于后续的二次开发。
隐式Intent的使用
相对于显式Intent,隐式Intent比较复杂。涉及到过滤器的概念:
intent过滤器是IntentFilter类的实例。然而,Android系统在启动组件前必须知道组件的能力,intent过滤器是在manifest文件(AndroidManifest.xml)里作为元素建立而不是在java代码里。(有一个特例是:广播接收器的过滤器,它是通过Context.registerReceiver()函数动态的注册;它被作为IntentFilter对象创建。)
一个过滤器有动作域、数据域、intent对象类别域。一个显式的intent测试这三个域。并派送给拥有过滤器的组件,必须通过三个测试。如果有一个测试失败,Android系统都不会分派——至少不是过滤器的基础。然而,如果组件有多个intent过滤器,不分派给一个组件也会分派给另一个组件。
具体的信息可以参考官方文档,这里不加赘述:
Google官方文档-Intent过滤器
下面我们来看看代码实现:
java文件中
Intent intent = new Intent();intent.setAction("android.intent.action.toSecond");startActivity(intent);
AndroidManifest文件中
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="android.intent.action.toSecond" /> <category android:name="android.intent.category.DEFAULT"/> </intent-filter></activity>
这里需要注意的是,只有和中的内容同时能够匹配上Intent中指定的action和category时,这个活动才能响应该Intent。
完。
- Intent(信使)显式与隐式调用
- android -- >intent (信使)
- Intent(二)隐式调用intent
- android intent 隐式调用
- Android 隐式调用 intent
- Intent的隐式调用
- 调用活动,通过intent显式调用
- activity的信使intent
- Android Intent信使使用
- android intent调用隐式调用系统
- 调用活动,通过intent隐式调用
- 显示Intent与隐式Intent
- 显式Intent与隐式Intent
- 【Android】显式Intent与隐式Intent
- 显式Intent与隐式Intent
- Android Intent隐式调用之intent-filter匹配规则
- Intent隐式与显式
- Intent的显式与隐式
- 個人記録
- Binder in Java
- Mac下统计任意文件夹中代码行数的工具——cloc
- Android手机文件远程管理
- Jmeter进行http接口测试(对原文补充)
- Intent(信使)显式与隐式调用
- Tomcat安装配置
- servlet中请求转发(forword)与重定向(sendredirect)的区别
- 中级Unity3D开发工程师-面试经验总结
- Element animation-list must be declared
- Binder In Native
- 如何选型商业智能和分析平台,Gartner给了这些建议!
- SpringAop在实际项目中的使用案例
- 静态类型语言和动态类型语言