天天看点

Android学习_广播

广播接收器也是运行在ui线程,因此,onreceive方法中不能执行太耗时的操作。

否则将因此anr。一般情况下,根据实际业务需求,

onreceive方法中都会涉及到与其他组件之间的交互,

如发送notification、启动service等。

当此activity实例化时,会动态将mybroadcastreceiver注册到系统中。

当此activity销毁时,动态注册的mybroadcastreceiver将不再接收到相应的广播。

 根据广播的发送方式,可以将其分为以下几种类型:

1.normal broadcast:普通广播

2.system broadcast: 系统广播

涉及到手机的基本操作,基本上都会发出相应的系统广播。如:开启启动,网络状态改变,

拍照,屏幕关闭与开启,点亮不足等等。每个系统广播都具有特定的intent-filter,

其中主要包括具体的action,系统广播发出后,将被相应的broadcastreceiver接收。

系统广播在系统内部当特定事件发生时,有系统自动发出。

3.ordered broadcast:有序广播

定义过程与普通广播无异,

只是其的主要发送方式变为:sendorderedbroadcast(intent, receiverpermission, ...)。

priority属性值从大到小排序,对于具有相同的priority的动态广播和静态广播,动态广播会排在前面。

4.sticky broadcast:粘性广播(在 android 5.0/api 21中deprecated,不再推荐使用,

相应的还有粘性有序广播,同样已经deprecated)

5.local broadcast:app应用内广播

常见的增加安全性的方案是:

1.对于同一app内部发送和接收广播,将exported属性人为设置成false,使得非本app内部发出的此广播不被接收;

2.在广播发送和接收时,都增加上相应的permission,用于权限验证;

3.发送广播时,指定特定广播接收器所在的包名,具体是通过intent.setpackage(packagename)指定在,

这样此广播将只会发送到此包中的app内与之相匹配的有效广播接收器中。

//registerreceiver(mbroadcastreceiver, intentfilter);

//注册应用内广播接收器

localbroadcastmanager = localbroadcastmanager.getinstance(this);

localbroadcastmanager.registerreceiver(mbroadcastreceiver, intentfilter);

//unregisterreceiver(mbroadcastreceiver);

//取消注册应用内广播接收器

localbroadcastmanager.unregisterreceiver(mbroadcastreceiver);

intent intent = new intent();

intent.setaction(broadcast_action);

intent.putextra("name", "qqyumidi");

//sendbroadcast(intent);

//发送应用内广播

localbroadcastmanager.sendbroadcast(intent);

1.//动态广播 注册和发送

/**

* 定义一个broadcastreceiver广播接收类:

*/

private broadcastreceiver mbroadcastreceiver = new broadcastreceiver() {

@override

public void onreceive(context context, intent intent) {

string action = intent.getaction();

if (action.equals(action_name)) {

toast.maketext(mainactivity.this, "处理action名字相对应的广播", 200);

}

};

* 注册广播

public void registerboradcastreceiver() {

intentfilter myintentfilter = new intentfilter();

myintentfilter.addaction(action_name);

// 注册广播

registerreceiver(mbroadcastreceiver, myintentfilter);//接收者对象、接受者action

1)先调用注册广播

2)发送广播

intent mintent = new intent(action_name);

mintent.putextra("txt", "发送广播,相当于在这里传送数据");

// 发送广播

sendbroadcast(mintent);

3)销毁

protected void ondestroy() {

// todo auto-generated method stub

super.ondestroy();

unregisterreceiver(recevier);

2.静态注册广播

2.1定义广播类继承 broadcastreceiver

log.i("tag", "normalbroadcastrecevier");

log.i("tag", "action=" + intent.getaction());

2.2在manifest中注册 广播action

<receiver android:name="???" >

            <intent-filter>

                <action android:name="com.jingtai" />

            </intent-filter>

        </receiver>

2.3发送广播

log.i("tag", "静态注册-正在发送正常广播....");

// 封装要启动的广播接收器的action

intent intent = new intent("com.jingtai");

sendbroadcast(intent);

3.有序广播 priority

3.1广播的注册方法:通过设置android:priority的数值,控制广播接收的优先级

<receiver android:name="com.??" >

            <intent-filter android:priority="80">

                <action android:name="com.order" />

3.2broadcastreceiver 中onreceive方法中的参数的传递

封装 bundle参数

bundle bundle = new bundle();

bundle.putstring("txt", "收到广播之后立马结束广播");

setresultextras(bundle);

接收 bundle参数

bundle bundle = this.getresultextras(true);

log.i("tag", "---->values=" + bundle.getstring("txt"));

1)创建 多了broadcastreceiver 对象

2)manifest 注册相同的action  不同的 android:priority= "数值"

3)启动调用  有序广播

// 封装id

intent.setaction("com.example.ch16_broadcast_abcd");

// 发送有序广播

sendorderedbroadcast(intent, null);

============================================

数据传递  有序广播接收者中的数据传递方法;

拦截有序广播  abortbroadcast();

priority 数值越大,优先级越高,即,越先接收到广播

<intent-filter android:priority="1000"> 

                 <action android:name="actionname" />

            </category></action></intent-filter>

string strmsg = intent.getstringextra("msg");

        log.e("pzf", "第一个:"+strmsg);

        bundle extras=new bundle();

        extras.putstring("msg", "第一个界面传过来的"+strmsg);

        setresultextras(extras); //继续向下川

//abortbroadcast();//开启此处,可以截断广播,不让它传到third中

        string strmsg = intent.getstringextra("msg"); //获取广播的原始数据

        log.e("pzf", "第二个:"+strmsg);

        extras.putstring("msg", "第二个界面传过来的"+strmsg);

        setresultextras(extras); //继续向下   

        setresultdata("第二个:"+strmsg); // "第二个:"+strmsg这是two中新的数据,传递到third中,在third中是用getresultdata来获得setresultdata("")中的数据

string resultdata = getresultdata();//获取two中setresultdata中的数据

        bundle bundle = getresultextras(true);//获取two中setresultextras中的数据

        string bundledata = bundle.getstring("msg", "");//获取two中setresultextras()中的数据 

        string strmsg = intent.getstringextra("msg");//获取广播的原始数据

        log.e("pzf", "第三个:"+strmsg); 

        log.e("pzf", "two中传到third新的数据:"+resultdata);

        log.e("pzf", "two中setresultextras(extras)传到third新的数据:"+bundledata);

==============================================

4.举例演示 service 和broadcast 结合刷新ui

4.1主页面注册 广播

private broadcastreceiver recevier = new broadcastreceiver() {

int value = intent.getintextra("key",0);

tv.settext(value + "%");

if (value == 100) {

btnstart.setenabled(true);

tv.settext("下载完成!");

4.2 注册广播的 intentfilter 

private void initintentfilter(){

intentfilter filter = new intentfilter();

filter.addaction("myservice_send inner_broadcast");

this.registerreceiver(recevier, filter);

4.3 启动服务类、模拟下载的线程

intent intent = new intent(mainactivity.this, myservice.class);

startservice(intent);

btnstart.setenabled(false);//设置按钮不可以点击

4.4 myservice 的实现类 模拟下载耗时操作 发送广播刷新ui

public class myservice extends service {

public ibinder onbind(intent intent) {return null;}

public void oncreate() {super.oncreate();}

public int onstartcommand(intent intent, int flags, int startid) {

// 模拟耗时操作

new thread(new runnable() {

public void run() {

int count = 0;

intent i = new intent();

i.setaction("myservice_send inner_broadcast");

while (count < 100) {

count++;

// 模拟多少时间之后才发送广播

try {

thread.sleep(100);

} catch (interruptedexception e) {

e.printstacktrace();

i.putextra("key", count);

// 开始发送广播

sendbroadcast(i);

}).start();

return super.onstartcommand(intent, flags, startid);

系统的广播的action,在离线api可以搜索

standard broadcast actions

these are the current standard actions that intent defines for 

receiving broadcasts (usually through registerreceiver(broadcastreceiver, 

intentfilter) or a <receiver> tag in a manifest).

action_time_tick

action_time_changed

action_timezone_changed

action_boot_completed

action_package_added

action_package_changed

action_package_removed

action_package_restarted

action_package_data_cleared

action_uid_removed

action_battery_changed

action_power_connected

action_power_disconnected

action_shutdown

继续阅读