1.BroadcastReceiver用来接收sendBroadcast()方法发出来的广播,可以通过Intent传递数据,它是一个抽象类
2、广播特点:数据的传递方向是单向的,调到固定的频率
3、安卓的广播作用范围有限,只在当前手机里有效
4、系统把一些重要的操作,通过广播的形式通知给所有的应用
广播接受者案例
1、 IP拨号器
1. 写一个类继承BroadcastReceiver,重写onReceive方法
2. 清单文件中注册receiver节点,通过intent-filter指定当前广播接收者要处理的广播事件
2、 SD卡状态监听
1. 需要监听挂载和卸载的action
2. SD卡状态变化的广播还需要加一个data,scheme是file,否则收不到广播
3. 同一个广播接收者接收了多个广播事件,可以通过action来区分
3、 短信监听
1.需要监听的action:
3.不同广播版本的特点
1、4.0之后,没有运行过的应用是不能收到广播的
2、在应用管理器中强制停止的应用,也收不到广播
四.1、卸载安装
可用于竞品分析
2、开机启动
五,
1、 发送无序广播:创建Intent,设置action,通过sendBroadcast(intent)就可以把广播发出去,当前的设备上只要有广播接收者注册了相同的action,就可以收到广播,并且在发广播的时候,可以通过Intent传递数据
2、接收无序广播:注册广播接收者,指定对应的action,就可以收到这个广播
有序广播:
可以通过priority设置接收顺序
abortBroadcast()可以中断
sendOrderedBroadcast()
无序广播:
大家一起收到
不可以中断
sendBroadcast()
3、如何区分有序广播和无序广播?接收到广播之后在onReceive方法中调用abortBroadcast()方法,如果没有异常说明是有序广播,如果报BroadcastReceiver trying to return result during a non-ordered broadcast异常说明是无序广播
六,特殊广播接受者
1、四大组件只有广播接收者可以不在清单文件中注册
2、静态注册:在清单文件中通过声明一个receiver节点指定intent-filter,这种方式就是静态注册
3、动态注册:registerReceiver(receiver, filter);
4、注销的方法:unregisterReceiver(receiver);
import android.content.BroadcastReceiver;
import android.content.Context;}
package com.example.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//监听SD卡挂在状态的广播接收者
public class SDCardStateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("android.intent.action.MEDIA_MOUNTED".equals(action)) {
Log.e("TAG", "SDCard已安装");
}else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Log.e("TAG", "SDCard已卸载");
}
// Log.e("TAG", "SDCard被操作");
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//接收自定义的广播
public class CustomReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "收到自定义广播"+intent.getStringExtra("key"));
abortBroadcast();
}
}
package com.example.orderbroadcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send (View v){
Intent intent = new Intent();
intent.setAction("com.zmh.sendrice");
//收到广播是需要权限
String receiverPermission = null;
//作为最终的广播接收者
BroadcastReceiver resultReceiver = new FinaReceiver();
//处理最终的广播接收者用到的Handler,如果传null会在主线程处理
Handler scheduler = null;
//初始化数据
String initialData = "皇帝发放赈灾粮,每人100斤";
sendOrderedBroadcast(intent, receiverPermission, resultReceiver, scheduler, Activity.RESULT_OK, initialData, null);
}
}
package com.example.orderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//钦差大臣
public class FinaReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Toast.makeText(context, "final:"+resultData, Toast.LENGTH_SHORT).show();
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
package com.example.receiverorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//州
public class StateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("皇帝发放赈灾粮,每人60斤");
// abortBroadcast();//终止广播继续发送,只有有序广播可以中断
}
}
package com.example.receiverorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//市
public class CityReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("皇帝发放赈灾粮,每人30斤");
}
}
package com.example.receiverorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//镇
public class TownReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("皇恩浩荡,今年受灾,免个人所得税");
}
}
package com.example.receiverorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//农民
public class FarmerReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
}
}
清单文件中添加:
}
package com.example.registerreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenLightReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("android.intent.action.SCREEN_ON".equals(action)) {
Log.e("TAG", "屏幕点亮");
}else if ("android.intent.action.SCREEN_OFF".equals(action)) {
Log.e("TAG", "屏幕关闭");
}
}
}