您的位置:首页 > 移动开发 > Android开发

图解Android广播机制

2014-12-31 09:48 253 查看
原文:http://www.cnblogs.com/TerryBlog/archive/2010/08/16/1801016.html

从现实生活中理解广播机制

一听到广播我们第一感觉就会联想到小时候村里面的广播,每逢村里有什么活动都是通过广播发送的。收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同。接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理。好比我们收听交通电台的广播,电台中告诉我们现在在交通状况如何,但它并不关心我们接收到广播时做如何做出处理,这不是广播应该关心的问题,OK,到这里我们从生活中的一些小例子浅浅的理解了一下广播,那么Android
中的广播是如何操作的呢?

Android 的广播机制

在 Android 里面有各种各样的广播,比如电池的使用状态,电话的接收和短信的接收都会产生一个广播,应用程序开发者也可以监听这些广播并做出程序逻辑的处理。下面我画一张粗略的图来帮助大家理解广播的运行机制。



Android 中有各式各样的广播,各种广播在Android 系统中运行,当系统/应用程序运行时便会向 Android 注册各种广播,Android 接收到广播会便会判断哪种广播需要哪种事件,然后向不同需要事件的应用程序注册事件,不同的广播可能处理不同的事件也可能处理相同的广播事件,这时就需要Android 系统为我们做筛选。

案例分析:

一个经典的电话黑名单,首先通过将黑名单号码保存在数据库里面,当来电时,我们接收到来电广播并将黑名单号码与数据库中的某个数据做匹配,如果匹配的话则做出相应的处理,比如挂掉电话、比如静音等等。。。



Demo 分析:

下面通过一个小DEMO 来讲解一下广播在Android 中如何编写,在Demo中我们设置了一个按钮为按钮设置点击监听通过点击发送广播,在后台中接收到广播并打印LOG信息。代码如下:
BroadCastActivity 页面代码 

<span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> BroadCastActivity </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">extends</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> Activity {
    </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">final</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> String ACTION_INTENT_TEST </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">=</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">com.terry.broadcast.test</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">;

    </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">/**</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;"> Called when the activity is first created. </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">
    @Override
    </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> onCreate(Bundle savedInstanceState) {
        </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">super</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">=</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> OnClickListener() {

            @Override
            </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> onClick(View v) {
                </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;"> TODO Auto-generated method stub</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">
</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">                Intent intent </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">=</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> Intent(ACTION_INTENT_TEST);
                sendBroadcast(intent);
            }
        });
    }
}</span>


***代码如下:

<span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> myBroadCast </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">extends</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> BroadcastReceiver {

     
    </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> myBroadCast() {
        Log.v(</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">BROADCAST_TAG</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">, </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">myBroadCast</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">);
    }

    @Override
    </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> </span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;"> onReceive(Context context, Intent intent) {
        </span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;"> TODO Auto-generated method stub</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); line-height: 1.5 !important;">
</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">        Log.v(</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">BROADCAST_TAG</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">, </span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">onReceive</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; line-height: 1.5 !important;">);
    }

}</span>


Android 广播的生命周期

在上面的***中,继承了BroadcastReceiver 并重写了它的onReceive 并构造了一个函数,下面通过图片来一步一步认识 Android 广播的生命周期。当我点击一下按钮,它向Android 发送了一个广播,如下图:



这时我们再点击一下按钮,它还是会再向 Android 系统发送广播,此时日志信息如下:



下面本人画一张图像,描述了Android 中广播的生命周期,其次它并不像Activity 一样复杂,运行原理很简单如下图:



下面来看一下SDK给出的解释:



大意为:如果一个广播处理完onReceive 那么系统将认定此对象将不再是一个活动的对象,也就会finished掉它。

至此,大家应该能明白 Android 的广播生命周期的原理,代码也不用多介绍,很简单的一个发送广播并处理广播的Demo。



Android 如何判断并筛选广播?

前面说过 Android 的广播有各式各样,那么Android 系统是如何帮我们处理我们需要哪种广播并为我们提供相应的广播服务呢?这里有一点需要大家注意,每实现一个广播接收类必须在我们应用程序中的 manifest 中显式的注明哪一个类需要广播,并为其设置过滤器,如下图:



Tip:action 代表一个要执行的动作,在Andriod 中有很action 比如 ACTION_VIEW,ACTION_EDIT

那么有些人会问了,如果我在一个广播***中要处理多个动作呢?那要如何去处理?
在Android 的***中onReceive 以经为我们想到的,同样的你必须在Intent-filter 里面注册该动作,可以是系统的广播动作也可以是自己需要的广播,之后你之需要在onReceive 方法中,通过intent.getAction()判断传进来的动作即可做出不同的处理,不同的动作。具体大家可以去尝试测试一下。

小结:

在Android 中如果要发送一个广播必须使用sendBroadCast 向系统发送对其感兴趣的广播***中。
使用广播必须要有一个intent 对象必设置其action动作对象
使用广播必须在配置文件中显式的指明该广播对象
每次接收广播都会重新生成一个接收广播的对象
在BroadCast 中尽量不要处理太多逻辑问题,建议复杂的逻辑交给Activity 或者 Service 去处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: