Android开发基础复习_2

0x00 前言

继续写“第一行代码”的读书笔记,这次看看Android的广播机制

0x01 广播机制

广播机制分为两类:

  • 标准广播:异步的,广播发送之后,所有的广播接收器几乎同时接受广播,广播效率比较高。但不能截断。

  • 有序广播:同步执行的,优先级高的先接受到,之前的广播接收器可以截断广播。

0x02 动态注册广播接收器和静态注册广播接收器

先来看如何写动态注册,关键函数是:registerReceiver(networkChangReceiver,intentFilter)

public class MainActivity extends AppCompatActivity {

private IntentFilter intentFilter;
private NetworkChangReceiver networkChangReceiver;

class NetworkChangReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
Toast.makeText(context,"Netwokchange",Toast.LENGTH_SHORT).show();
}

}

protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangReceiver);//注销广播接收器
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//定义广播接收器的接收动作
networkChangReceiver = new NetworkChangReceiver();//新建一个广播接收器
registerReceiver(networkChangReceiver,intentFilter);//动态注册广播接收器

静态注册的编写方法就简单多了,直接定义一个接收类,

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.
Toast.makeText(context, "开机 ", Toast.LENGTH_SHORT).show();
throw new UnsupportedOperationException("Not yet implemented");
}
}

在AndroidManifest.xml中注册,添加Intentfileter;并且开启权限:RECEIVE_BOOT_COMPLETED

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

0x03 发送广播

  • 发送标准广播

基本步骤为:新建一个广播接收器,在然后AndroidManifest.xml中注册,添加Intentfileter,发送自定义广播,也就是Intent。

这里的广播接收器我用上面的就OK了,只需要添加一个Intentfileter就完成。

//发送标准广播到我们定义的广播接收器
Button button5 = (Button) findViewById(R.id.button_5);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.broadcastdemo.MY_BROADCAST");//还是要用到Intent
sendBroadcast(intent);
  • 发送有序广播

发送广播方式改为:sendOrderedBroadcast(intent, null);


Button button5 = (Button) findViewById(R.id.button_5);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.broadcastdemo.MY_BROADCAST");
sendOrderedBroadcast(intent,null);//发送有序广播
}
});

广播截断:abortBroadcast();

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.
Toast.makeText(context, "接收信息成功", Toast.LENGTH_SHORT).show();
abortBroadcast(); //截断广播
}
}
  • 本地广播

前面解释的都是全局广播,其他应用程序可以接收,本地广播只能够在应用程序的内部进行传递,有效更加安全性。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
Button button5 = (Button) findViewById(R.id.button_5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.broadcastdemo.MY_BROADCAST_LOCAL");
//发送本地广播
localBroadcastManager.sendBroadcast(intent);
}
});

静态注册本地广播接收器:localBroadcastManager.registerReceiver(localReceiver,intentFilter);

intentFilter = new IntentFilter();
//定义广播接收器的接收动作
intentFilter.addAction("com.example.broadcastdemo.MY_BROADCAST_LOCAL");

localReceiver = new LocalReceiver();
//静态注册本地广播接收器
localBroadcastManager.registerReceiver(localReceiver,intentFilter);