一、简介
在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理。
二、三要素
广播(Broadcast) - 用于发送广播;
广播接收器(BroadcastReceiver) - 用于接收广播;
意图内容(Intent)-用于保存广播相关信息的媒介。
三、生命周期
广播接收器仅在它执行这个方法时处于活跃状态。当onReceive()返回后,它即为失活状态。
拥有一个活跃状态的广播接收器的进程被保护起来而不会被杀死,但仅拥有失活状态组件的进程则会在其它进程需要它所占有的内存的时候随时被杀掉。所以,如果响应一个广播信息需要很长的一段时间,我们一般会将其纳入一个衍生的线程中去完成,而不是在主线程内完成它,从而保证用户交互过程的流畅。
四、创建步骤
1、 创建要发送的广播
<span style="font-size:12px;">private void initButton() {
hahaButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent hahaIntent = new Intent("haha");
hahaIntent.putExtra("hahaha", "哈哈哈");
// 发送广播
sendBroadcast(hahaIntent);
}
});
heheButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent heheIntent = new Intent("hehe");
heheIntent.putExtra("hehehe", "呵呵呵");
sendBroadcast(heheIntent);
}
});
}</span>
2、 创建广播接收器
<span style="font-size:12px;">private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("haha")) {
Toast.makeText(
MainActivity.this,
"处理action名字相对应的广播,收到的信息是:"
+ intent.getStringExtra("hahaha"), 200).show();
} else if (action.equals("hehe")) {
Toast.makeText(
MainActivity.this,
"处理heheIntent的广播,收到的信息是:"
+ intent.getStringExtra("hehehe"), 200).show();
}
}
};</span>
3、注册广播
<span style="font-size:12px;">public void registerBoradcastReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("haha");
myIntentFilter.addAction("hehe");
registerReceiver(mBroadcastReceiver, myIntentFilter);
}</span>
五、全部代码
<span style="font-size:12px;">package com.example.android_my_broadcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button hahaButton;
private Button heheButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initButton();
registerBoradcastReceiver();
}
// 初始化UI
private void initUI() {
hahaButton = (Button) findViewById(R.id.button1);
heheButton = (Button) findViewById(R.id.button2);
hahaButton.setText("发送广播:哈哈");
heheButton.setText("发送广播:呵呵");
}
// button点击事件
private void initButton() {
hahaButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent hahaIntent = new Intent("haha");
hahaIntent.putExtra("hahaha", "哈哈哈");
// 发送广播
sendBroadcast(hahaIntent);
}
});
heheButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent heheIntent = new Intent("hehe");
heheIntent.putExtra("hehehe", "呵呵呵");
sendBroadcast(heheIntent);
}
});
}
// 接收广播
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("haha")) {
Toast.makeText(
MainActivity.this,
"处理action名字相对应的广播,收到的信息是:"
+ intent.getStringExtra("hahaha"), 200).show();
} else if (action.equals("hehe")) {
Toast.makeText(
MainActivity.this,
"处理heheIntent的广播,收到的信息是:"
+ intent.getStringExtra("hehehe"), 200).show();
}
}
};
// 注册广播
public void registerBoradcastReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("haha");
myIntentFilter.addAction("hehe");
registerReceiver(mBroadcastReceiver, myIntentFilter);
}
}
</span>
运行效果: