Intent原理
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作设计数据、附加数据进行描述。Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。通过Intent还能将数据封装起来,传递到需要传递的Activity中,再在目标Activity中获取到,进行使用。
要激活一个新的Activity,或者让一个现有的Activity做新的操作,比如ActivityA需要和ActivityB进行联系,二者并不需要直接联系,而是通过Intent作为桥梁。通俗来讲,Intent类似于中介、媒婆的角色。
实例:
实现从一个页面跳转到另一个页面的以及打开i浏览器的功能。
效果:
- 主界面 activity_main.xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开一个新的界面"
/>
<Button
android:id="@