Android基础之广播接收者

创建广播接收者

电量不足,sd卡被移除,电话外拨,短息到了等等各种各样的事件…

1,创建一个类继承BroadcastReceiver

class SDCardStatusReceiver extends BroadcastReceiver {

 }

2,在清单文件中的Application节点下进行配置

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    ...
    ...
    <!-- 广播接收者的全类名 -->
    <receiver android:name="com.sdstatus.SDCardStatusReceiver" >
        <!-- 配置要接收哪个广播 -->
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
            <data android:scheme="file"></data>
        </intent-filter>
    </receiver>
</application>

3,在onreceive方法里面编写业务逻辑

public class SDCardStatusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "外部存储卡已被拔出,微信头像和朋友圈功能暂不可用", 0).show();
    }
}
开机启动的广播接收者的配置

1,添加权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

2,在清单文件的application节点中进行配置

<receiver android:name="com.bootcomplete.BootCompleteReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
外拨电话的广播接收者的配置

1,添加权限

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

2,在清单文件的application节点中进行配置

<receiver android:name="com.ipdail.OutCallRecevier">
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter>
</receiver>

3,获得外拨电话号,修改外拨电话号

public class OutCallRecevier extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sp = context.getSharedPreferences("config", 0);
        String ipnumber = sp.getString("ipnumber", "");

        //获取外拨电话号
        String number = getResultData();
        if (number.startsWith("0")) {

            //设置外拨电话号
            setResultData(ipnumber + number);
        }
    }

}
软件安装和卸载的广播接收者的配置
  • 在清单文件的application节点中进行配置
<receiver android:name="com.datacollection.AppStatusReceiver" >
    <intent-filter >
        <action android:name="android.intent.action.PACKAGE_ADDED"/>
        <action android:name="android.intent.action.PACKAGE_REMOVED"/>
        <data android:scheme="package"></data>
    </intent-filter>
</receiver>
短信广播接收者的配置

1,添加权限

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

2,在清单文件的application节点中进行配置

<receiver android:name="com.smslistener.SmsReceiver">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

  • 短信数据的解析
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("短信到来了....");
        //短信的数据是pdu的数据,必须对短信的格式很了解才可以解析短信.
        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for(Object obj:objs){
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
            String body = smsMessage.getMessageBody();
            String sender = smsMessage.getOriginatingAddress();
            System.out.println("body:"+body);
            System.out.println("sender:"+sender);
            abortBroadcast();
        }
    }
}

广播接收者不同版本中的特性
自Android 3.1之后,所有新安装但未被执行过的apk,以及那些被用户强行停止的apk,都会处于stopped状态。这个状态下,apk中的广播接收者,均处于未激活状态, 无法履行监听功能。
在广播发送方发送广播时需要设置Intent.FLAGINCLUDESTOPPED_PACKAGES
    Intent intent = new Intent();
    intent.setAction("com.broadcast");
    if (android.os.Build.VERSION.SDK_INT >= 12) {
         intent.setFlags(32);//3.1以后的版本需要设置Intent.FLAG_INCLUDE_STOPPED_PACKAGES
    }
    sendBroadcast(intent);


如何发送和接收自定义广播

1,发送

Intent intent = new Intent();
//自定义action
intent.setAction("com.ccav.XWLB");
//可以添加参数
intent.putExtra("key", "dfafadfa");
sendBroadcast(intent);

2,接收

<receiver android:name="com.myreceiver.MyReceiver">
    <intent-filter >
        //填写要接收自定义广播的action
        <action android:name="com.ccav.XWLB"/>
    </intent-filter>
</receiver>  
有序广播和无序广播的区别

有序广播
广播消息是按照一定的顺序传达的,高优先级的先得到广播消息,低优先级的后得到
高优先级的可以拦截广播消息或者修改广播消息
效率比较低
无序广播
广播消息没有顺序,同时接受广播消息
不可以修改广播消息
效率高

如何发送有序广播
Intent intent = new Intent();
intent.setAction("com.gov.SENDMASHROOM");
// 发送有序广播
// intent 意图
// receiverPermission 权限 默认null
// resultReceiver 结果接受者 null
// scheduler 消息处理器 null 默认
// initialCode 初始化码
// initialData 初始化数据
// initialExtras null 额外的数据
sendOrderedBroadcast(intent, null, null, null, 1, "主席讲话: 每人10斤蘑菇", null);


有序广播的特点

1,优先级在哪里配置,范围是多少

<receiver android:name="com.smslistener.SmsReceiver">
    //在此处配置,范围从1000到-1000
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

2,如何获取数据,如何修改数据

//获取数据
String data = getResultData();
//修改数据
setResultData("主席讲话:每人5斤蘑菇");

3,如何终止有序广播

abortBroadcast();

4,resultReceiver什么时候收到广播,需要在清单文件中配置吗

* 在所有广播接收者都收到消息后接受到广播
* 即使有广播接收者终止了广播,resultReceiver也可以接收到广播
* 不需要在清单文件中配置

5,如何动态注册广播接收者

receiver = new ScreenStatusReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_OFF");
filter.addAction("android.intent.action.SCREEN_ON");
registerReceiver(receiver, filter);

动态注册广播接收者的特点

在android里面有一些产生非常频繁的广播事件,在清单文件里面配置是不会生效。电量变化、屏幕锁屏、解锁,这些广播事件只能利用代码注册
动态注册的广播接收者可以将其关闭掉

unregisterReceiver(receiver);
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值