一、BroadcastReceiver是什么?
BroadcastReceiver就是应用程序间的全局大喇叭,即通信的一个手段, 系统自己在很多时候都会发送广播,比如电量低或者充足,刚启动完,插入耳机,输入法改变等, 发生这些时间,系统都会发送广播,这个叫系统广播,每个APP都会收到。
我们可以设置自己的应用在收到系统指定的广播后进行一些操作,或者发送自己的广播
二、接收系统广播
接收系统广播有以下两种方式:
1.静态注册
先定义前台按钮
<!-- 静态注册广播 发送广播 区域 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="静态注册广播 发送广播 区域"
android:textSize="20dp"
android:textColor="@color/black"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="sendAction2"
/>
后台:
第一步,在Manifest.xml中静态注册广播接收者
<!-- 第一步:静态注册广播接收者 -->
<receiver android:name=".CustomReceiver">
<intent-filter>
<action android:name="com.derry.receiver_flag_" />
</intent-filter>
</receiver>
第二步,发送给接收者
// TODO ===================== 静态发送广播区域
// 第二步:发送给接收者
// 静态发送广播 给 接收者
public void sendAction2(View view) {
Intent intent = new Intent();
// ActionUtils.ACTION_FLAG 与注册时保持一致
intent.setAction(ActionUtils.ACTION_FLAG);
sendBroadcast(intent);
}
2.动态注册
定义前台按钮:
<!-- 动态注册广播 发送广播 区域 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态注册广播 发送广播 区域"
android:textSize="20dp"
android:textColor="@color/design_default_color_secondary"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="sendAction1"
/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
// Java 代码 来注册 刚刚的接收者即可
// 第二步,在onCreate注册广播(订阅)
// 动态使用Java代码注册一个广播接收者
UpdateIpSelectCity updateIpSelectCity = new UpdateIpSelectCity();
IntentFilter filter = new IntentFilter();
filter.addAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
registerReceiver(updateIpSelectCity, filter);
}
// 第三步
// 发送给 动态注册的接收者
public void sendAction1(View view) {
Intent intent = new Intent();
// ActionUtils.ACTION_EQUES_UPDATE_IP 与注册时保持一致
intent.setAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
sendBroadcast(intent);
}
三、发送广播
发送广播前先定义一个接收器MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "com.example.broadcasttest.MY_BROADCAST";
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, "收到告白啦~",Toast.LENGTH_SHORT).show();
}
}
然后Manifest.xml中注册下接收者,写上Intent-filter:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
在MainActivity里完成广播发送:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendBroadcast(new Intent("com.example.broadcasttest.MY_BROADCAST"));
}
});
}
}