zoukankan      html  css  js  c++  java
  • Android广播机制(两种注册方法)与 中断广播

      两种注册类型的区别是:

         1)第一种不是常驻型广播,也就是说广播跟随activity的生命周期。注意: 在activity结束前,移除广播接收器。

         2)第二种是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。

    在android下,要想接受广播信息,那么这个广播接收器就得我们自己来实现了,我们可以继承BroadcastReceiver,就可以有一个广播接受器了。有个接受器还不够,我们还得重写BroadcastReceiver里面的onReceiver方法,当来广播的时候我们要干什么,这就要我们自己来实现,不过我们可以搞一个信息防火墙。具体的代码:

    public class SmsBroadCastReceiver extends BroadcastReceiver    
    {   
      
        @Override  
        
    public void onReceive(Context context, Intent intent)   
        {   
            Bundle bundle 
    = intent.getExtras();   
            Object[] object 
    = (Object[])bundle.get("pdus");   
            SmsMessage sms[]
    =new SmsMessage[object.length];   
            
    for(int i=0;i<object.length;i++)   
            {   
                sms[
    0= SmsMessage.createFromPdu((byte[])object[i]);   
                Toast.makeText(context, 
    "来自"+sms[i].getDisplayOriginatingAddress()+" 的消息是:"+sms[i].getDisplayMessageBody(), Toast.LENGTH_SHORT).show();   
            }   
            
    //终止广播,在这里我们可以稍微处理,根据用户输入的号码可以实现短信防火墙。   
            abortBroadcast();   
        }   
           
    }  

      当实现了广播接收器,还要设置广播接收器接收广播信息的类型,这里是信息:android.provider.Telephony.SMS_RECEIVED

      我们就可以把广播接收器注册到系统里面,可以让系统知道我们有个广播接收器。这里有两种,一种是代码动态注册:

    //生成广播处理   
    smsBroadCastReceiver = new SmsBroadCastReceiver();   
    //实例化过滤器并设置要过滤的广播   
    IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); 

    //注册广播   
    BroadCastReceiverActivity.this.registerReceiver(smsBroadCastReceiver, intentFilter);  

    一种是在AndroidManifest.xml中配置广播

    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
          package
    ="spl.broadCastReceiver"  
          android:versionCode
    ="1"  
          android:versionName
    ="1.0">  
        
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
            
    <activity android:name=".BroadCastReceiverActivity"  
                      android:label
    ="@string/app_name">  
                
    <intent-filter>  
                    
    <action android:name="android.intent.action.MAIN" />  
                    
    <category android:name="android.intent.category.LAUNCHER" />  
                
    </intent-filter>  
            
    </activity>  
               
            
    <!--广播注册-->  
            
    <receiver android:name=".SmsBroadCastReceiver">  
                
    <intent-filter android:priority="20">  
                    
    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
                
    </intent-filter>  
            
    </receiver>  
               
        
    </application>  
           
        
    <uses-sdk android:minSdkVersion="7" />  
           
        
    <!-- 权限申请 -->  
        
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>  
           
    </manifest>   

    http://www.cnblogs.com/TerryBlog/archive/2010/08/16/1801016.html // 详细讲解广播

  • 相关阅读:
    hbase深入了解
    Android SDK安装与环境变量配置以及开发第一个Android程序
    Android平台架构及特性
    android Notification定义与应用
    Windows Phone 7 开发 31 日谈——第1日:项目模板
    MVC HtmlHelper类的方法总结(转)
    INotifyPropertyChanged的使用
    浅谈计算机软件的破解与保护(时间:20111224作者:李富云 来源:中国论文库)
    托管代码&非托管代码
    .net也疯狂:生成zip文件(转)
  • 原文地址:https://www.cnblogs.com/error404/p/2126034.html
Copyright © 2011-2022 走看看