BroadCast用于接收其他程序或者本身发出的系统级别的通知
分为标准广播和有序广播
又分为动态注册和静态注册
动态注册:
首先新建一个类用于接收广播(即广播接收者)的类,该类继承BroadCatReceiver类,并且重写onReceiver方法,收到广播后的各种逻辑就是在该方法里面编写,在onCreate()方法里面回去IntentFilter实例,并且添加广播的指令,然后获取BroadCastReciver类的实例,然后利用registerReciver()方法进行广播的注册,
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_BroadCast = (Button) findViewById(R.id.bt_BroadCast); bt_BroadCast.setOnClickListener(new MyOnclick()); intentFilter = new IntentFilter(); newBroadCast = new NewBroadCast(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(广播对象,IntentFilter对象 ); }
动态注册的广播一定要取消注册,即在onDestroy()方法中调用unregisterReceiver()方法取消注册
@Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(newBroadCast); }
静态注册:
静态注册就是新建类继承BroadCastReciver类,然后在该类的清单文件中中指定发送的命令,就是静态注册广播
<receiver android:name=".MyBrpadCastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="指定发送的命令"/> </intent-filter> </receiver>
发送自定义广播:
标准广播
布局设计一个按钮,在按钮的点击事件中定义Intent并且发送出去,
public class MyOnclick implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_BroadCast: Intent intent = new Intent("ca.sd.zsl.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);//发送标准广播
break; default: break; } } }
然后新建一个类MyBrpadCastReceiver继承BroadCastReciver,用于接收发送的广播,
不要忘记在清单文件中注册该广播
<receiver android:name=".MyBrpadCastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="ca.sd.zsl.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
发送有序广播:
发送有序广播和发送标准广播的大体步骤是一样的,
唯一的区别在于发送有序广播的时候使用的是sendOrderedBroadcast(intent,null)方法,
public class MyOnclick implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_BroadCast: Intent intent = new Intent("ca.sd.zsl.broadcasttest.MY_BROADCAST"); sendOrderedBroadcast(intent,null);//发送有序广播 break; default: break; } } }
在发送有序广播时,可以在创建一个项目,并且新建类继承BroadCastReciver类,在清单文件中同样配置相同的action命令,即可接受相同的广播,
有序广播可以设置接收广播的的优先级,在清单文件中设置 android:priority="100"属性,数值越大,代表优先级越大,
<receiver android:name=".AnotherBroadCastreciver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="ca.sd.zsl.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
想要截断有序广播的传输,就在要截断的那个继承BroadCastReciver的类重写的方法onReciver()中最后添加一行代码abortBroadcast();
@Override public void onReceive(Context context, Intent intent) { // 编写接受到广播后的操作逻辑结构 abortBroadcast();//截断广播 }
发送本地广播:
该程序发出的广播只能在程序内部进行传递,而且该广播接收器也只能接受来自本程序发出的广播,并且本地广播不能在清单文件中注册,发送方式和动态注册广播接收器以及发送广播是大体一样的,
不过本地广播是通过LocalBroadcastManager.getInstance()方法得到一个实例,然后发送广播的时候调用 localBroadcastManager的sendBroadcast(intent)方法,然后取消注册广播的时候在调用localBroadcastManager的unregisterReceiver(localReciver)的方法。
package ca.sd.zsl.localbroadcastmanagetest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button bt_local; private IntentFilter intentFilter; private LocalReciver localReciver; private LocalBroadcastManager localBroadcastManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); localBroadcastManager = LocalBroadcastManager.getInstance(this);//获取一个本地实例 bt_local = (Button) findViewById(R.id.bt_local); bt_local.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent("ca.sd.zsl.LOCAL_BROADCAST"); localBroadcastManager.sendBroadcast(intent);//发送本地广播 } }); intentFilter = new IntentFilter(); intentFilter.addAction("ca.sd.zsl.LOCAL_BROADCAST"); localReciver = new LocalReciver(); localBroadcastManager.registerReceiver(localReciver, intentFilter);//注册本地广播件监听器 } @Override protected void onDestroy() { super.onDestroy(); localBroadcastManager.unregisterReceiver(localReciver); } public class LocalReciver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "广播", Toast.LENGTH_SHORT).show(); } } }