zoukankan      html  css  js  c++  java
  • Android基础之广播


    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 


    广播主要与Service共用,通知Service进行相应操作。


    首先写一个过滤器字段,随便定义,如android.provider.Telephony.SMS_RECEIVED。

    IntentFilter intentFilter = newIntentFilter("android.provider.Telephony.SMS_RECEIVED");


    //注册广播   
    BroadCastReceiverActivity.this.registerReceiver(smsBroadCastReceiver,intentFilter);  
    一种是在AndroidManifest.xml中配置广播

    <?xml version="1.0"encoding="utf-8"?>  
    <manifestxmlns: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>  
                    <actionandroid:name="android.intent.action.MAIN" />  
                    <categoryandroid:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
           </activity>  
               
           <!--广播注册-->  
           <receiver android:name=".SmsBroadCastReceiver">  
                <intent-filterandroid:priority="20">  
                    <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>  
                </intent-filter>  
           </receiver>  
               
       </application>  
          
       <uses-sdk android:minSdkVersion="7" />  
          
       <!-- 权限申请 -->  
       <uses-permissionandroid:name="android.permission.RECEIVE_SMS"></uses-permission>  
          
    </manifest>   

    两种注册类型的区别是:

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

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


    常驻型广播与Service一样,即使应用关闭,它仍然存在,会连续进行监控,有则通知操作,如不再使用则要注销广

    ;非常驻型广播只在当前Activity存在的情况下起作用,如通知其他Activity常量发生改变等;如不再使用则需要在

    ondestroy()方法里注销它。

    关于这两种广播,可以区别使用!

  • 相关阅读:
    Win7系统重启网卡批处理
    第一个应用程序HelloWorld
    JS异步流程控制(序列模式、并发模式、有限并发模式)
    bootstrap+MVC3在Moon.Orm中的应用(含有代码下载)
    google guava使用例子/示范
    证明Hadoop工作的正确性和可靠性只需4步图文并茂的过程
    python 图 自身遍历 及弱引用使用
    界面布局决定系统设计的成败
    .NET:栈的生长与消亡.
    IIS 6 & Server.MapPath
  • 原文地址:https://www.cnblogs.com/fengju/p/6174509.html
Copyright © 2011-2022 走看看