Intent 是一种消息传播机制,用于组件之间数据交换和发送广播消息。通过本次实验了解 Android 系统的组件通信原理,掌握利用 Intent 启动其他组件的方法,以及利用 Intent 获取信息和发送广播消息的方法。
1、实现具有“登录”按钮的主界面,输入用户名、密码,点击登录按钮后,经过判断进入一个广播Activity(需要传递主界面的用户名)
2、在广播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="1"
android:layout_gravity="right"
android:layout_column="0"
android:text="密码"/>
<EditText
android:ems="16"
android:layout_columnSpan="3"
android:id="@+id/password"/>
<Button
android:text="登录"
android:id="@+id/signin"
android:layout_column="1"
android:layout_gravity="fill_horizontal"/>
<Button
android:text="退出"
android:id="@+id/signout"
android:layout_column="2"
android:layout_gravity="fill_horizontal"/>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_my_brocast_reveicer.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=".MySendBrocastReceiver">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:columnCount="4">
<LinearLayout
android:layout_columnSpan="1"
android:orientation="horizontal">
<TextView
android:layout_gravity="left"
android:text="欢迎你"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:layout_gravity="left"
android:id="@+id/name"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
<EditText
android:ems="16"
android:layout_column="0"
android:layout_columnSpan="3"
android:id="@+id/text"/>
<Button
android:text="发送广播"
android:id="@+id/send"
android:layout_column="0"
android:layout_gravity="fill_horizontal"/>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MyReceiver.java
package com.example.intendbrocastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
String name=intent.getStringExtra("name");
Toast.makeText(context,name,Toast.LENGTH_LONG).show();
}
}
MySendBrocastReceiver.java
package com.example.intendbrocastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MySendBrocastReceiver extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_send_brocast_receiver);
TextView text=(TextView)findViewById(R.id.name);//文本框对象
//获取用户名
Intent getuser=getIntent();
String s=getuser.getStringExtra("user");
text.setText(s);
//动态注册广播
MyReceiver myreceicer=new MyReceiver();
IntentFilter intentfilter=new IntentFilter();
intentfilter.addAction("com.example.intentdbrocastreceiver.send");
registerReceiver(myreceicer,intentfilter);
Button but_send=(Button)findViewById(R.id.send);
but_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText text=(EditText)findViewById(R.id.text);
String te=text.getText().toString();
Intent intent=new Intent();
intent.setAction("com.example.intentdbrocastreceiver.send");
intent.putExtra("name",te);//传递
sendBroadcast(intent);//发送广播
}
});
}
}
MainActivity.java
package com.example.intendbrocastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin=(Button)findViewById(R.id.signin);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText users=(EditText)findViewById(R.id.user);
EditText passwords=(EditText)findViewById(R.id.password);
//用户输入的用户名密码
String user=users.getText().toString();
String password=passwords.getText().toString();
//系统内包含的用户名密码
String myuser="123";
String mypassword="666";
if(user.equals(myuser)&&password.equals(mypassword)){
Intent login=new Intent();
login.setAction("android.intent.action.sendbrocast");
login.putExtra("user",user);//传递用户名
startActivity(login);
}else{
Toast.makeText(MainActivity.this,"用户名不存在或密码错误",Toast.LENGTH_SHORT).show();
}
}
});
Button out=(Button)findViewById(R.id.signout);
out.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}