1.BroadcastReceiver定义
2.BroadcastReceiver作用(能干嘛?)
3.BroadcastReceiver两种注册方式
4.BroadcastReceiver分类及对比
5.BroadcastReceiver发送与接收
6.常用的系统广播
2.BroadcastReceiver定义:
BroadcastReceiver(广播接收者),相当于人的“耳朵”,听别人说话,然后我们做出响应,那么话由谁来传递呢,那就是Intent(android信使)
它相当于我们说话声音的传播媒介。
3.BroadcastReceiver作用(能干嘛?)
明白了它的定义,当然作用我相信大家也知道了,它的作用主要是接受系统以及其他应用发的广播,然后应用做出相应的响应(例如:
电量低广播,开机启动广播,网络状态广播 等等)
4.BroadcastReceiver两种注册方式
4.1 静态注册(xml注册)
4.1.1 发送广播类(MainActivity.java)
package com.eandy.staticbroadcastreceiver;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send);
// 设置按钮监听
send.setOnClickListener(new OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
// 调用send()函数发送广播
send();
Log.i("click", "button clicked");
}
});
}
private void send() {
// 采用隐式Intent发送广播()
Intent intent = new Intent("android.intent.action.BROADCAST");
intent.putExtra("broadcast", "Hello! I'm a broadcast,Did you receive?");
// 发送广播
sendBroadcast(intent);
}
}
4.1.2 接收广播类(MyStaticBroadcastReceiver.java)
package com.eandy.staticbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyStaticBroadcastReceiver extends BroadcastReceiver {
/*
* onReceive()方法实现接收广播操作
*/
@Override
public void onReceive(Context context, Intent intent) {
// 获取广播数据
String data = intent.getStringExtra("broadcast");
// 打印到LOGCAT
Log.i("MyStaticBroadcastReceiver", "MyStaticBroadcastReceiver-->"
+ data);
}
}
4.1.3 广播注册(Manifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eandy.staticbroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<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>
<receiver android:name=".MyStaticBroadcastReceiver" >
<intent-filter>
<!-- 匹配隐式intent的action -->
<action android:name="android.intent.action.BROADCAST" />
<!-- 匹配隐式intent启动 -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
>
</application>
</manifest>
4.1.4 截图
4.2 动态注册(代码注册)
4.2.1 发送广播类
package com.andy.dynamicbroadcastreceiver;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send);
//注册动态BroadcastReceiver
MyDynamicBroadcastReceiver receiver= new MyDynamicBroadcastReceiver();
IntentFilter filter =new IntentFilter("android.intent.action.BROADCAST");
registerReceiver(receiver, filter);
// 设置按钮监听
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 调用send()函数发送广播
send();
}
});
}
private void send() {
// 采用隐式Intent发送广播()
Intent intent = new Intent("android.intent.action.BROADCAST");
intent.putExtra("broadcast", "Hello! I'm a broadcast,Did you receive?");
// 发送广播
sendBroadcast(intent);
}
}
4.2.2 接收广播类
package com.andy.dynamicbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyDynamicBroadcastReceiver extends BroadcastReceiver {
/*
* onReceive()方法实现接收广播操作
*/
@Override
public void onReceive(Context context, Intent intent) {
// 获取广播数据
String data = intent.getStringExtra("broadcast");
// 打印到LOGCAT
Log.i("MyDynamicBroadcastReceiver", "MyDynamicBroadcastReceiver-->"
+ data);
}
}
4.2.2 截图
静态注册即使程序没有运行也能收到广播并进行相应处理,而动态注册只有在程序运行的时候才能收到广播
5.BroadcastReceiver分类及对比
5.1 普通广播
5.1.1 定义:所有广播平等,无先后顺序,并不会给谁优待,同时收到广播。(注意:普通广播中所有接收者无法干涉其他
接收者时候接收广播,也就是无法中断其他接收者接收广播)
5.2 有序广播
5.2.1 定义:接收者有优先级,关系好(优先级高)先收到,同时是一个传递过程,第一个传递给第二个以此类推。(注意:在传递过程中
前面的接收者可修改甚至中断广播,阻止后面的接收者接收广播)
5.3 有序与普通广播区别案例
5.3.1 MainActivity.java
package com.andy.normalandorderedbroadcasts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button normalButton;
Button orderedButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 绑定button
normalButton = (Button) findViewById(R.id.normalButton);
orderedButton = (Button) findViewById(R.id.orderedButton);
// 设置normalButton监听
normalButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
send(v);
}
});
// 设置orderedButton事件监听
orderedButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
send(v);
}
});
}
/*
* 点击发送广播方法
*/
public void send(View v) {
switch (v.getId()) {
case R.id.normalButton:
// 声明初始化Intent
Intent intent = new Intent(
"android.intent.action.MY_NORMAL_BROADCAST");
// 添加额外传输数据
intent.putExtra("data", "hello I'm a normal broadcast");
// 发送广播
sendBroadcast(intent);
Log.i("MY_NORMAL_BROADCAST", "send a normal broadcast");
break;
case R.id.orderedButton:
// 声明初始化Intent
Intent intent1 = new Intent(
"android.intent.action.MY_ORDERED_BROADCAST");
// 添加额外传输数据
intent1.putExtra("msg", "hello I'm a ordered broadcast");
// 发送广播 第二个参数是manifest.xml文件中定义的权限,定义在发送广播的manifest.xml中
sendOrderedBroadcast(intent1, "andy.permission.MY_ORDERED_BROADCAST");
Log.i("MY_ORDERED_BROADCAST", "send a ordered broadcast");
break;
default:
break;
}
}
}
5.3.2 FirstNormalRecevier.java
package com.andy.normalandorderedbroadcasts.normalbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class FirstNormalRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获得intent数据
String data =intent.getStringExtra("data");
Log.i("MY_NORMAL_BROADCAST", "FirstNormalRecevier->"+data);
}
}
5.3.3 SecondNormalRecevier.java
package com.andy.normalandorderedbroadcasts.normalbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class SecondNormalRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获得intent数据
String data =intent.getStringExtra("data");
Log.i("MY_NORMAL_BROADCAST", "SecondNormalRecevier->"+data);
}
}
5.3.4 ThirdNormalRecevier.java
package com.andy.normalandorderedbroadcasts.normalbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ThirdNormalRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获得intent数据
String data =intent.getStringExtra("data");
Log.i("MY_NORMAL_BROADCAST", "ThirdNormalRecevier->"+data);
}
}
5.3.5 FirstOrderedRecevier.java
package com.andy.normalandorderedbroadcasts.orderedbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class FirstOrderedRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取intent传递数据
String msg = intent.getStringExtra("msg");
//String msg = getResultExtras(true).getString("msg");
Log.i("MY_ORDERED_BROADCAST", "FirstOrderedRecevier:"+msg);
Bundle bundle =new Bundle();
bundle.putString("extraDate", msg+"+FirstOrderedRecevier extraDate");
//Bundle对象设置为结果集对象,传入下一个接收者
setResultExtras(bundle);
}
}
5.3.6 SecondOrderedRecevier.java
package com.andy.normalandorderedbroadcasts.orderedbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SecondOrderedRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//String data = intent.getStringExtra("data");获取不到附加数据
//获取intent和接收者附加数据集
String msg = getResultExtras(true).getString("extraDate");
Log.i("MY_ORDERED_BROADCAST", "SecondOrderedRecevier:"+msg);
Bundle bundle =new Bundle();
bundle.putString("extraDate", msg+"+SecondOrderedRecevier extraDate");
//Bundle对象设置为结果集对象,传入下一个接收者
setResultExtras(bundle);
}
}
5.3.7 ThirdOrderedRecevier.java
package com.andy.normalandorderedbroadcasts.orderedbroadcastrecevier;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class ThirdOrderedRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//String data = intent.getStringExtra("data");获取不到附加数据
//获取intent和接收者附加数据集
String msg = getResultExtras(true).getString("extraDate");
Log.i("MY_ORDERED_BROADCAST", "ThirdOrderedRecevier:"+msg);
}
}
5.3.8 普通广播运行Log
5.3.9 有序广播运行Log
ps:有序广播附加参数的特点上面代码已经展现,终止广播继续传播的特点,就不再演示,伙伴们可以试着在FirstOrderedRecevier.java/SecondOrderedRecevier.java中添加如下代码,运行测试
abortBroadcast(); //中断广播继续发送给下一个接收者
6.1 BroadcastReceiver的发送
// 采用隐式Intent发送广播()
Intent intent = new Intent("android.intent.action.BROADCAST");
intent.putExtra("broadcast", "Hello! I'm a broadcast,Did you receive?");
// 发送广播
sendBroadcast(intent);
6.2 BroadcastReceiver的接收
/*
* onReceive()方法实现接收广播操作
*/
@Override
public void onReceive(Context context, Intent intent) {
// 获取广播数据
String data = intent.getStringExtra("broadcast");
// 打印到LOGCAT
Log.i("MyDynamicBroadcastReceiver", "MyDynamicBroadcastReceiver-->"
+ data);
}
7.常用的系统广播(参考自,雨季o莫忧离:http://blog.csdn.net/luckkof)
Intent.ACTION_AIRPLANE_MODE_CHANGED; ——————> 关闭或打开飞行模式时的广播
Intent.ACTION_BATTERY_CHANGED; ——————> 充电状态,或者电池的电量发生变化广播
Intent.ACTION_BATTERY_LOW;——————> 表示电池电量低时广播
Intent.ACTION_BATTERY_OKAY;——————> 表示电池电量充足,即从电池电量低变化到饱满时会发出广播
Intent.ACTION_BOOT_COMPLETED;——————> 在系统启动完成后广播(只有一次)
Intent.ACTION_CAMERA_BUTTON;——————> 按下照相时的拍照按键(硬件按键)时发出的广播
Intent.ACTION_CLOSE_SYSTEM_DIALOGS;——————> 当屏幕超时进行锁屏,用户按下电源按钮,长按或短按时进行锁屏时广播
Intent.ACTION_CONFIGURATION_CHANGED;——————> 设备当前设置被改变时发出的广播
Intent.ACTION_DATE_CHANGED;——————> 设备日期改变时广播
Intent.ACTION_DEVICE_STORAGE_LOW;——————> 内存不足时广播
Intent.ACTION_DEVICE_STORAGE_OK;——————> 内存从不足到充足时广播
Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE;——————> 移动APP完成之后广播
Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE;——————> 正在移动APP时广播
Intent.ACTION_GTALK_SERVICE_CONNECTED;——————> Gtalk已建立连接时广播
Intent.ACTION_GTALK_SERVICE_DISCONNECTED;——————> Gtalk已断开连接时广播
Intent.ACTION_HEADSET_PLUG;——————> 插入耳机时广播
Intent.ACTION_INPUT_METHOD_CHANGED;——————> 改变输入法时广播
Intent.ACTION_LOCALE_CHANGED;——————> 当前区域设置已更改时广播
Intent.ACTION_MANAGE_PACKAGE_STORAGE;
Intent.ACTION_MEDIA_BAD_REMOVAL;——————> 未正确移除SD卡时广播
Intent.ACTION_MEDIA_BUTTON;——————> 按下"Media Button" 按键时广播
Intent.ACTION_MEDIA_CHECKING;——————> 插入外部储存装置时广播
Intent.ACTION_MEDIA_EJECT;——————> 拔掉外部大容量储存设备广播
Intent.ACTION_MEDIA_MOUNTED;——————> 插入SD卡并正确安装时广播
Intent.ACTION_MEDIA_NOFS;——————>
Intent.ACTION_MEDIA_REMOVED;——————> 外部储存设备移除广播
Intent.ACTION_MEDIA_SCANNER_FINISHED;——————> 已经扫描完介质的一个目录时广播
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE;——————>
Intent.ACTION_MEDIA_SCANNER_STARTED;——————>开始扫描介质的一个目录时广播
Intent.ACTION_MEDIA_SHARED;——————> 扩展介质的挂载被解除
Intent.ACTION_MEDIA_UNMOUNTABLE;——————>
Intent.ACTION_MEDIA_UNMOUNTED;——————>扩展介质存在,但没有被挂载时广播
Intent.ACTION_NEW_OUTGOING_CALL;——————>
Intent.ACTION_PACKAGE_ADDED;——————> 成功的安装APK之后
Intent.ACTION_PACKAGE_CHANGED;——————> 已存在的应用程序包改变时广播
Intent.ACTION_PACKAGE_DATA_CLEARED;——————> 清除一个应用程序的数据时广播
Intent.ACTION_PACKAGE_INSTALL;——————> 触发一个下载并且完成安装时发出的广播
Intent.ACTION_PACKAGE_REMOVED;——————> 成功的删除某个APK之后发出的广播
Intent.ACTION_PACKAGE_REPLACED;——————> 替换一个现有的安装包时广播
Intent.ACTION_PACKAGE_RESTARTED;——————> 用户重新开始一个包
Intent.ACTION_POWER_CONNECTED;——————> 插上外部电源时广播
Intent.ACTION_POWER_DISCONNECTED;——————> 断开外部电源连接时广播
Intent.ACTION_PROVIDER_CHANGED;——————>
Intent.ACTION_REBOOT;——————> 重启设备时的广播
Intent.ACTION_SCREEN_OFF;——————> 屏幕被关闭之后的广播
Intent.ACTION_SCREEN_ON;——————> 屏幕被打开之后的广播
Intent.ACTION_SHUTDOWN;——————> 关闭系统时广播
Intent.ACTION_TIMEZONE_CHANGED;——————> 时区改变时广播
Intent.ACTION_TIME_CHANGED;——————> 时间被设置时广播
Intent.ACTION_TIME_TICK;——————> 当前时间已变化,通过Context.registerReceiver()注册
Intent.ACTION_UID_REMOVED;——————> 某用户ID已从系统中移除广播
Intent.ACTION_UMS_CONNECTED;——————>设备进入USB大容量储存状态时广播
Intent.ACTION_UMS_DISCONNECTED;——————>设备已从USB大容量储存状态转为正常状态时广播
Intent.ACTION_USER_PRESENT;
Intent.ACTION_WALLPAPER_CHANGED;——————> 设备墙纸已改变时广播