Android-Broadcast详解与汇总

来源:互联网 发布:mac flamingo 价格 编辑:程序博客网 时间:2024/05/16 00:41

一、广播的实现

1、监听短信的广播

 

public class SmsBroadcastReceiver extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent) {        Object[] pdus=(Object[])intent.getExtras().get("pdus");                for(Object pdu:pdus)        {            byte[] date=(byte[])pdu;            SmsMessage message=SmsMessage.createFromPdu(date);                        String sender=message.getOriginatingAddress();            String body=message.getMessageBody();                        if(sender.equals(AppUtil.herPhone)&&body.regionMatches(0, AppUtil.herSmsText, 0, 18))            {                //Toast.makeText(context, body, Toast.LENGTH_LONG).show();                String [] bodyArray=body.split(" ");                String longitude=bodyArray[3];                String latitude=bodyArray[4];                                Intent uiIntent=new Intent();                Bundle bundle=new Bundle();                bundle.putString("longitude", longitude);                bundle.putString("latitude", latitude);                                uiIntent.putExtras(bundle);                uiIntent.setAction("android.janmac.location");                Log.v("bundle", uiIntent.getExtras().getString("longitude"));                context.sendBroadcast(uiIntent);                                abortBroadcast();                            }        }            }}

 

 

 

 

2、嵌套在Activity内的广播实现,多用在更新Activity的UI界面。

private  class UIReceiver extends BroadcastReceiver    {        @Override        public void onReceive(Context context, Intent intent) {            Log.v("location","uireceiver接受成功!");                        Bundle bundle=new Bundle();            bundle=intent.getExtras();            herLongitude=Double.valueOf(bundle.getString("longitude"));            herLatitude=Double.valueOf(bundle.getString("latitude"));                        HerLocationInit();            button.setText("获取成功!");        }            }

二、注册广播接收

1、静态注册

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

 

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

 

2、动态注册

uiReceiver=new UIReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("android.janmac.location");
registerReceiver(uiReceiver, filter);

 

三、发送广播

  

  首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用Context.sendBroadcast()、sendOrderBroadcast()或sendStickyBroadcast()方法,把 Intent对象以广播方式发送出去。 
     

  当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若 匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。 

 

Intent uiIntent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("longitude", longitude);
bundle.putString("latitude", latitude);

uiIntent.putExtras(bundle);
uiIntent.setAction("android.janmac.location");
Log.v("bundle", uiIntent.getExtras().getString("longitude"));
context.sendBroadcast(uiIntent);

 

四、怎么用好 BroadcastReceiver ?

每次广播到来时,会重新创建 BroadcastReceiver 对象,并且调用 onReceive() 方法 , 执行完以后,该对象即被销毁。当 onReceive() 方法在 10 秒内没有执行完毕, Android 会认为该程序无响应。所以在BroadcastReceiver 里不能做一些比较耗时的操作 , 否侧会弹出 ANR(Application NoResponse) 的对话框。

如果需要完成一项比较耗时的工作, 应该通过发送 Intent 给 Service, 由 Service 来完成。这里不能使用子线程来解决,因为 BroadcastReceiver 的生命周期很短 , 子线程可能还没有结束BroadcastReceiver 就先结束了。

BroadcastReceiver 一旦结束,此时 BroadcastReceiver 的所在进程很容易在系统需要内存时被优先杀死,因为它属于空进程 ( 没有任何活动组件的进程 )。 如果它的宿主进程被杀死,那么正在工作的子线程也会被杀死。所以采用子线程来解决是不可靠的。

 

五、其他

Service通过广播Broadcast更新Activity UI

http://www.eoeandroid.com/thread-68005-1-1.html

关于有序广播等其他方面-Android 中的BroadCastReceiver

http://yangguangfu.iteye.com/blog/1063732

原创粉丝点击