1 在 activity_main.xml文件中添加一个 button 和 textview
<?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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
app:layout_constraintBottom_toTopOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.656" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="NULL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.431" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 在 MainActivity.java 代码的 onCreate 函数中获取button 和 textview 的实例
btn1 = findViewById(R.id.button1);
textView1 = findViewById(R.id.textView1);
3 创建button的监听器
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
4 在监听器内,创建Intent对象,设置Intent对象的action属性
Intent intent = new Intent();
intent.setAction("abc"); // action属性是接收广播数据的标识,注册了相同action属性的广播接收器才能收到发送的广播数据
5 在监听器内,发送需要广播的信息
编写需要广播的信息内容,将需要播发的信息封装到Intent中,通过Activity或Service继承其父类Context的sendBroadcast()方法将Intent广播出去
intent.putExtra("message", "广播消息的内容是:测试广播");
sendBroadcast(intent);
6 编写一个继承BroadcastReceiver的子类作为广播接收器,即TestReceiver.java文件
该对象是接收广播信息并对信息进行处理的组件。在子类中要重写接收广播信息的onReceive()方法。
这里注意该子类的创建方式:
右击MainActivity.java所在的包->New->Other->Broadcast Receiver
package XXX;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class TestReceiver extends BroadcastReceiver {
private String msg;
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// throw new UnsupportedOperationException("Not yet implemented");
msg = intent.getStringExtra("message");
Log.i("message",msg+""); // 输出信息到控制台
context.startActivity(intent); // 这一步非常关键
}
}
广播接收者将接收到的信息通过startActivity的方式传递给需要的activity界面(不耗时),然后再让那些activity界面进行耗时的处理操作
同时应该注意到的是,context.startActivity(intent)这句话如果想要成功实现,还需7.2的相关配置
7 (手动)在配置文件AndroidManifest.xml中注册广播接收类。
7.1 在android节点下,添加如下代码
<service android:name=".TestReceiver">
<intent-filter>
<action android:name="abc" />
</intent-filter>
</service>
7.2 在合适的activity节点下,增加相应配置
即添加下面这两行配置
action android:name=“abc”
让activity能接收到action为abc的广播消息
category android:name=“android.intent.category.DEFAULT”
每一个通过startActivity()方法发出的隐式Intent都至少有一个category,就是是"android.intent.category.DEFAULT"(如果不进行此配置会导致Intent 匹配失败)
示例如下:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="abc" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
8 模拟广播接收者,根据广播信息,在activity实现的相关耗时行为
public class MainActivity extends AppCompatActivity {
private Button btn1;
private TextView textView1;
private TestReceiver testReceiver;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.button1);
textView1 = findViewById(R.id.textView1);
if(this.getIntent().getAction()=="abc") {
Bundle bundle = this.getIntent().getExtras();
String res = bundle.getString("message");
textView1.setText(res);
}else {
textView1.setText("NULL");
}
}
}
9 完整的MainActivity.java代码
package XXX;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private TextView textView1;
private TestReceiver testReceiver;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.button1);
textView1 = findViewById(R.id.textView1);
// 模拟广播接收者
if(this.getIntent().getAction()=="abc") {
Bundle bundle = this.getIntent().getExtras();
String res = bundle.getString("message");
textView1.setText(res);
}else {
textView1.setText("NULL");
}
// 模拟广播接收者 结束
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 下面这段代码是模拟广播发送者
Intent intent = new Intent();
intent.setAction("abc"); // action属性是接收广播数据的标识,注册了相同action属性的广播接收器才能收到发送的广播数据
intent.putExtra("message", "广播消息的内容是:测试广播");
sendBroadcast(intent);
// 模拟发送者结束
// 下面这段代码是模拟广播接收者
testReceiver = new TestReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("abc");
registerReceiver(testReceiver, intentFilter);
// 模拟接收者结束
}
});
}
}
10 总结
关键文件是:
MainActivity.java【9】
TestReceiver.java【6】
AndroidManifest.xml【7】
activity_main.xml【1】
11 知识补充
Android系统在执行onReceive()方法时,会自动启动一个程序计时器,在一定时间内,广播接收器的实例会被销毁。因此,广播机制不适合传递大数据量的信息。
运行结果显示