Android随笔之——Android广播机制Broadcast详解

  在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理。这个广播跟我们传统意义中的电台广播有些相似之处。之所以叫做广播,就是因为它只负责“说”而不管你“听不听”,也就是不管你接收方如何处理。另外,广播可以被不只一个应用程序所接收,当然也可能不被任何应用程序所接收。

 

一、Android广播机制三要素:

  1、广播(Broadcast):用于发送广播。是一种广泛应用的在应用间传输信息的机制

  2、广播接收器(BroadcastReceiver):用于接收广播。是对发出来的Broadcast进行过滤接受并响应的组件。

  3、意图内容(Intent):用于保存广播相关信息的媒介

 

二、广播的功能和特征:

  1、广播的生命周期很短,经过 调用对象—实现onReceive—结束 整个过程就结束了。从实现的复杂度和代码量来看,广播无疑是最迷你的Android 组件,实现往往只需几行代码。广播对象被构造出来后通常只执行BroadcastReceiver.onReceive方法,便结束了其生命周期。所以有的时候我们可以把它当做函数看也未必不可。

  2、和所有组件一样,广播对象也是在应用进程的主线程中被构造,所以广播对象的执行必须是要同步且快速的。也不推荐在里面开子线程,因为往往线程还未结束,广播对象就已经执行完毕被系统销毁。如果需要完成一项比较耗时的工作 , 应该通过发送 Intent 给 Service, 由 Service 来完成。

  3、每次广播到来时 , 会重新创建 BroadcastReceiver 对象 , 并且调用 onReceive() 方法 , 执行完以后 , 该对象即被销毁 . 当 onReceive() 方法在 10 秒内没有执行完毕, Android 会认为该程序无响应。

 

三、广播的两种注册方式:

  1、常驻型广播:常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称作静态注册。这种方式可以理解为通过清单文件注册的广播是交给操作系统去处理的。示例代码如下: 

<receiver android:name=".AlarmReceiver" ><!-- Reveiver名称,如果是内部类静态注册广播,请在内部类前加$ -->
    <intent-filter>
        <action android:name="android.intent.action.ALARM_RECEIVER" /><!-- 广播接收的Intent -->

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

  新建一个AlarmReceiver,继承BroadcastReceiver

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Toast.makeText(arg0, "我是闹钟,我要叫醒你...", Toast.LENGTH_SHORT).show();  
    }
}

  最后创建一个简单的Activity就可以了,在里面发送一个静态广播就行了

 1 package com.example.alarmmanager;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 
 7 public class MainActivity extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13 
14         // 发送一个静态广播
15         Intent intent = new Intent("android.intent.action.ALARM_RECEIVER");
16         sendBroadcast(intent);
17     }
18 
19 }

  2、非常驻型广播:非常驻型广播,当应用程序结束了,广播自然就没有了,比如在 Activity 中的 onCreate 或者 onResume 中注册广播接收者,在 onDestory 中注销广播接收者。这样你的广播接收者就一个非常驻型的了,这种注册方式也叫动态注册。这种方式可以理解为通过代码注册的广播是和注册者关联在一起的。比如写一个监听 SDcard 状态的广播接收者:

 1 package com.example.alarmmanager;
 2 
 3 import android.app.Activity;
 4 import android.content.BroadcastReceiver;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.content.IntentFilter;
 8 import android.os.Bundle;
 9 import android.os.Environment;
10 
11 public class MainActivity extends Activity {
12     SdcardStateChanageReceiver sdcardStateReceiver;
13 
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16 
17         // 新建一个BroadcastReceiver
18         sdcardStateReceiver = new SdcardStateChanageReceiver();
19         // 添加意图过滤器
20         IntentFilter filter = new IntentFilter();
21         // 给filter添加Action,表示只接收这几个Action的广播
22         filter.addAction(Intent.ACTION_MEDIA_REMOVED);
23         filter.addAction(Intent.ACTION_MEDIA_EJECT);
24         filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
25         // 设置SDCard插拔接收
26         filter.addDataScheme("file");
27         // 注册一个动态广播
28         registerReceiver(sdcardStateReceiver, filter);
29     }
30 
31     protected void onDestroy() {
32         // 在摧毁Activity的时候,一定要记得注销广播,不然会报错
33         unregisterReceiver(sdcardStateReceiver);
34     }
35 
36     // BroadcastReceiver
37     class SdcardStateChanageReceiver extends BroadcastReceiver {
38         public void onReceive(Context context, Intent intent) {
39             String state = Environment.getExternalStorageState();
40             System.out.println(state);
41             if (state.equals(Environment.MEDIA_REMOVED)
42                     || state.equals(Environment.MEDIA_UNMOUNTED)) {
43                 System.out.println("SDCard 已卸载!");
44             }
45         }
46     }
47 }

 

作者:登天路

转载请说明出处:http://www.cnblogs.com/travellife/

posted @ 2014-08-29 10:32  登天路  阅读(5098)  评论(0编辑  收藏  举报