zoukankan      html  css  js  c++  java
  • Android 广播机制BroadCast

    http://www.cnblogs.com/TerryBlog/archive/2010/08/16/1801016.html

    在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>   

      两种注册类型的区别是:

         1)第一种不是常驻型广播,也就是说广播跟随程序的生命周期。

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

  • 相关阅读:
    POJ 2236 Wireless Network(并查集)
    POJ 2010 Moo University
    POJ 3614 Sunscreen(贪心,区间单点匹配)
    POJ 2184 Cow Exhibition(背包)
    POJ 1631 Bridging signals(LIS的等价表述)
    POJ 3181 Dollar Dayz(递推,两个long long)
    POJ 3046 Ant Counting(递推,和号优化)
    POJ 3280 Cheapest Palindrome(区间dp)
    POJ 3616 Milking Time(dp)
    POJ 2385 Apple Catching(01背包)
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1938896.html
Copyright © 2011-2022 走看看