Android四大组件之 BroadCastReceive 使用总结
BroadCast的定义:
BroadCast的分类:
BroadCase的使用:
一.普通广播:根据注册方式不同可以分为 静态注册广播和动态注册广播。
1.静态注册广播使用实例
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xjp.mybroadcast"> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--静态注册广播,此处需要添加广播的action--> <receiver android:name=".BroadCastReceive1"> <intent-filter> <action android:name="com.xjp.mybroadcast.BroadCastReceive1"></action> </intent-filter> </receiver> </application> </manifest>
2.广播接收者实现如下:
package com.xjp.mybroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; /** * Description:静态广播接收器 * User: xjp * Date: 2015/5/14 * Time: 14:56 */ public class BroadCastReceive1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //TODO 接收到广播之后处理自己的事情 String action = intent.getAction(); String result = intent.getStringExtra("key"); MyLog.d("the BroadCast action is " + action + " the BroadCast receive result is " + result); } }
3.广播发送者实现如下:
package com.xjp.mybroadcast; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = (Button) findViewById(R.id.button); btnSend.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: sendBraodCast(); break; } } private void sendBraodCast() { /** * 申明静态广播的 action 行为 */ String action = "com.xjp.mybroadcast.BroadCastReceive1"; Intent intent = new Intent(action); intent.putExtra("key", "静态广播测试"); sendBroadcast(intent); } }
打印结果如下:
4.静态注册广播特点:
二.动态广播:
示例
<pre name="code" class="java">package com.xjp.mybroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button btnSend; private BroadCastReceive2 myReceive; private IntentFilter filter; private final static String ACTION = "com.xjp.mybroadcast.BroadCastReceive2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = (Button) findViewById(R.id.button); btnSend.setOnClickListener(this); myReceive = new BroadCastReceive2(); filter = new IntentFilter(); filter.addAction(ACTION); } @Override protected void onResume() { /** * 注册广播 */ LocalBroadcastManager.getInstance(this).registerReceiver(myReceive, filter);//官方建议使用 // this.registerReceiver(myReceive, filter); super.onResume(); } @Override protected void onPause() { /** * 注销广播 */ LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceive);//官方建议使用 // this.unregisterReceiver(myReceive); super.onPause(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: sendBraodCast(); break; } } private void sendBraodCast() { /** * 申明广播的 action 行为 */ Intent intent = new Intent(ACTION); intent.putExtra("key", "动态广播测试"); /** * 官方提倡使用如下发送广播,原因是更快,更安全,不会导致内存泄漏 */ LocalBroadcastManager.getInstance(this).sendBroadcast(intent); // this.sendBroadcast(intent); } /** * 内部类实现广播接收器 */ private class BroadCastReceive2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //TODO 接收到广播之后处理自己的事情 String action = intent.getAction(); String result = intent.getStringExtra("key"); MyLog.d("the BroadCast action is " + action + " the BroadCast receive result is " + result); } } }
打印结果如下:
动态广播特点:
三.有序广播:
示例:
<pre name="code" class="java">package com.xjp.mybroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button btnSend; private BroadCastReceive2 myReceive; private BroadCastReceive3 myReceive3; private IntentFilter filter; private IntentFilter filter3; private final static String ACTION = "com.xjp.mybroadcast.BroadCastReceive2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = (Button) findViewById(R.id.button); btnSend.setOnClickListener(this); myReceive = new BroadCastReceive2(); filter = new IntentFilter(); filter.addAction(ACTION); filter.setPriority(2);//设置广播的优先级, -1000~1000 ,数字越大,优先级越高。 myReceive3 = new BroadCastReceive3(); filter3 = new IntentFilter(); filter3.addAction(ACTION); filter3.setPriority(1); } @Override protected void onResume() { /** * 注册广播 */ registerReceiver(myReceive, filter); registerReceiver(myReceive3, filter3); super.onResume(); } @Override protected void onPause() { /** * 注销广播 */ unregisterReceiver(myReceive); unregisterReceiver(myReceive3); super.onPause(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: sendBraodCast(); break; } } private void sendBraodCast() { /** * 申明广播的 action 行为 */ Intent intent = new Intent(ACTION); intent.putExtra("key", "有序广播测试"); this.sendOrderedBroadcast(intent, null); } /** * 内部类实现广播接收器 */ private class BroadCastReceive2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //TODO 接收到广播之后处理自己的事情 String action = intent.getAction(); String result = intent.getStringExtra("key"); MyLog.d("the BroadCast action is " + action + " the BroadCast receive result is " + result); Bundle bundle = new Bundle(); bundle.putString("key", "有序广播处理之后" + "\n" + "再次发送给下一个广播接收者"); intent.putExtra("bundle", bundle); setResultExtras(bundle); //切断广播,不再让此广播继续往下发送。 // abortBroadcast(); } } /** * 内部类实现广播接收器 */ private class BroadCastReceive3 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //TODO 接收到广播之后处理自己的事情 String action = intent.getAction(); //要不要接受上一个广播接收器receiver2传来的的数据 Bundle bundle = getResultExtras(true); MyLog.d("the BroadCast action is " + action + " the BroadCast receive result is " + bundle.getString("key")); } } }
有序广播特点:
四.粘性广播:
<uses-permission android:name="android.permission.BROADCAST_STICKY"></uses-permission>
package com.xjp.mybroadcast; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; /** * 发送广播的Activity */ public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button btnSend; private final static String ACTION = "com.xjp.mybroadcast.BroadCastReceive1"; private final static String ACTION1 = "com.xjp.mybroadcast.BroadCastReceive2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = (Button) findViewById(R.id.button); btnSend.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: sendBraodCast(); break; } } private void sendBraodCast() { /** * 申明广播的 action 行为 */ Intent intent = new Intent(ACTION); intent.putExtra("key", "普通广播测试"); sendBroadcast(intent); Intent intent1 = new Intent(ACTION1); intent1.putExtra("key", "粘性广播测试"); sendStickyBroadcast(intent1); startActivity(new Intent(this, RecevieActivity.class)); } }接受广播的Activity
package com.xjp.mybroadcast; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; /** * Description:接受广播的Activity * User: xjp * Date: 2015/5/14 * Time: 17:03 */ public class RecevieActivity extends Activity { private final static String ACTION1 = "com.xjp.mybroadcast.BroadCastReceive1"; private final static String ACTION2 = "com.xjp.mybroadcast.BroadCastReceive2"; private Receive receive; private IntentFilter filter1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); receive = new Receive(); filter1 = new IntentFilter(); filter1.addAction(ACTION1); filter1.addAction(ACTION2); } @Override protected void onResume() { super.onResume(); registerReceiver(receive, filter1); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receive); } private class Receive extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); String result = intent.getStringExtra("key"); MyLog.d("the BroadCast action is " + action + " the BroadCast receive result is " + result); } } }
打印结果如下:
粘性广播特点:
<uses-permission android:name="android.permission.BROADCAST_STICKY"></uses-permission>2.粘性广播发送除了调用方法不同sendStickyBroadcast(intent1),其他都一样。
3.一般广播都是先注册广播,才能接收到广播,而粘性广播可以做到先发送广播,哪里需要接收该广播就哪里注册,可以后注册广播拿到广播的结果。这就是 普通广播和粘性广播的区别。从示例中也看出了普通广播在跳转到ReceiveActivity中是接受不到广播发送者发出的广播的,只有粘性广播才能接收到。
// Register for the battery changed event IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); / Intent is sticky so using null as receiver works fine // return value contains the status Intent batteryStatus = this.registerReceiver(null, filter); // Are we charging / charged? int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging? int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
广播的生命周期:
广播的大概原理:
系统广播:
Event | Description |
---|---|
Intent.ACTION_BOOT_COMPLETED |
Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission. |
Intent.ACTION_POWER_CONNECTED | Power got connected to the device. |
Intent.ACTION_POWER_DISCONNECTED | Power got disconnected to the device. |
Intent.ACTION_BATTERY_LOW | Triggered on low battery. Typically used to reduce activities in your app which consume power. |
Intent.ACTION_BATTERY_OKAY | Battery status good again. |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。