- 定义
广播,是一个全局的监听器,属于Android四大组件之一.
Android 广播分为两个角色:广播发送者、广播接收者
2.广播接收器注册
注册的方式分为两种:静态注册、动态注册
静态注册:在AndroidManifest.xml里通过<receive>标签声明
动态注册:在代码中调用Context.registerReceiver()方法
3.广播接收代码案例(监听网络状态,动态注册)
①自定义一个类继承BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取网络管理器类
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
Toast.makeText(context, "有网!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "没网!", Toast.LENGTH_SHORT).show();
}
}
}
②动态注册广播
public class MainActivity extends AppCompatActivity {
private MyReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播
receiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//接收网络变化
registerReceiver(receiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);//注销广播
}
}
③AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4.广播接收(开启状态,静态注册)
①静态注册
public class MyReceiver2 extends BroadcastReceiver {
//广播接收器:接收系统开机广播
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "开机完成!", Toast.LENGTH_SHORT).show();
}
}
②AndroidManifest.xml,创建广播时选择New——Other——Broadcast Receiver会自动生成下面的标签
<receiver
android:name=".reciver.MyReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
5.广播发送
广播的类型主要分为5类:
普通广播(Normal Broadcast)
系统广播(System Broadcast)
有序广播(Ordered Broadcast)
粘性广播(Sticky Broadcast)
App应用内广播(Local Broadcast
下面只介绍了普通广播(标准广播)和有序广播
1.创建3个MyReceiver
public class MyReceiver01 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver01", Toast.LENGTH_SHORT).show();
}
}
public class MyReceiver02 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver02", Toast.LENGTH_SHORT).show();
abortBroadcast();//拦截广播
}
}
public class MyReceiver03 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver03", Toast.LENGTH_SHORT).show();
}
}
2.布局文件activity_main.xml
<Button
android:text="标准"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_send"/>
<Button
android:text="无序"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
3.MainActivity.java中
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_send;
private Button btn_sendOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
btn_send = (Button) findViewById(R.id.btn_send);
btn_sendOrder = (Button) findViewById(R.id.btn_sendOrder);
btn_send.setOnClickListener(this);
btn_sendOrder.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send:
Intent intent = new Intent("com.ABC");
sendBroadcast(intent);//发送标准广播
break;
case R.id.btn_sendOrder:
Intent intent2 = new Intent("com.ABC");
sendOrderedBroadcast(intent2, null);//发送有序广播
break;
}
}
}
3.清单文件,AndroidManifest.xml
<receiver
android:name=".reciver2.MyReceiver01"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="80">
<action android:name="com.ABC" />
</intent-filter>
</receiver>
<receiver
android:name=".reciver2.MyReceiver02"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="90">
<action android:name="com.ABC" />
</intent-filter>
</receiver>
<receiver
android:name=".reciver2.MyReceiver03"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">//priority是设置优先级,值越大越靠前
<action android:name="com.ABC" />
</intent-filter>
</receiver>
网友评论