Intent 是一种消息传播机制,用于组件之间数据交换和发送广播消息。通过本次实验了解 Android 系统的组件通信原理,掌握利用 Intent 启动其他组件的方法,以及利用 Intent 获取信息和发送广播消息的方法。
- 实现具有“登录”按钮的主界面,输入用户名、密码,点击登录按钮后,经过判断进入一个广播Activity(需要传递主界面的用户名)
- 在广播Activity中,输入信息,点击发送广播按钮发送广播,并且在广播接收器中接收广播并显示。
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:columnCount="4">
<TextView
android:layout_columnSpan="1"
android:layout_gravity="right"
android:text="用户名"/>
<EditText
android:ems="16"
android:layout_columnSpan="3"
android:id="@+id/user"/>
<TextView
android:layout_columnSpan="