码迷,mamicode.com
首页 > 移动开发 > 详细

android入门到熟练(五)---广播

时间:2016-04-22 16:16:38      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

1.广播类型:标准广播和有序广播。标准广播是异步广播在广播发出之后所有接收器几乎会同一时刻接收到,没有先后顺序,效率高,但无法被截断。有序广播则是同步广播,同一时刻只能一个接收器接收这条消息,等执行完毕后才会继续传递,优先级高的接收器可以先接受,并且可以截断该广播传递。

动态注册案例分析:网络变化提醒
构建响应类:
    class  NetworkChangeReciver extends BroadcastReceiver{
           @Override        
           public void onReceive(Context context,Intent intent){
                    ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();    
                    if ((networkInfo!=null&&networkInfo.isAvailable())) {
                                Toast.makeText(context,"网络可用",Toast.LENGTH_SHORT).show();
                           } else{     
                                     Toast.makeText(context,"网络不可用",Toast.LENGTH_SHORT).show();
                           }
        }
创建活动时注册响应,并且分类过滤消息
    private IntentFilter intentFilter;//过滤消息
    private NetworkChangeReciver networkChangeReciver;
    @Override
      protected void onCreate(Bundle savedInstanceState) {   
                           super.onCreate(savedInstanceState);       
                           setContentView(R.layout.activity_main);
                           intentFilter=new IntentFilter();  
                           intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");   
                           networkChangeReciver=new NetworkChangeReciver();     
                           registerReceiver(networkChangeReciver,intentFilter);
       }
销毁监听器
    @Override    
     protected void onDestroy() {
             super.onDestroy();  
            unregisterReceiver(networkChangeReciver);
       }
 
最后在配置文件中加入权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 
静态注册案例分析(好处是可以在不启动程序的情况监听广播)开机启动
 
定义一个监听类
public class BootCompleteReceiver extends BroadcastReceiver {     @Override     public void onReceive(Context context,Intent intent)     {         Toast.makeText(context,"启动完成",Toast.LENGTH_SHORT).show();     } }
修改android配置文件
<?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.zhb.test2" >     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application         android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >         <receiver android:name="com.example.zhb.Com.BootCompleteReceiver">             <intent-filter>                 <action android:name="android.intent.action.BOOT_COMPLETED"/>             </intent-filter>         </receiver>
      <activity             android:name=".MainActivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> < /manifest>
 
 
2.发送标准广播和有序广播
标准广播定义一个接收广播类
public class MyBroadcastReceiver extends BroadcastReceiver {     @Override     public void onReceive(Context context,Intent intent)     {         String value=intent.getStringExtra("key");         Toast.makeText(context,value,Toast.LENGTH_SHORT).show();     } }
 
配置配置文件信息
        <receiver android:name="com.example.zhb.Com.MyBroadcastReceiver">             <intent-filter>                 <action android:name="com.example.broadcasttest.MY_BROADCAST"/>             </intent-filter>         </receiver>
发送带数据的消息
        Button btnBRCTestOne = (Button) findViewById(R.id.btnBRCTestOne);         btnBRCTestOne.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");                 intent.putExtra("key","我是广播消息哦!");                 sendBroadcast(intent);             }         }
 
有序消息:只需改变一行代码
 @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.brocasttest);
        Button btnBRCTestOne = (Button) findViewById(R.id.btnBRCTestOne);         btnBRCTestOne.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");                 intent.putExtra("key","我是广播消息哦!");                 //sendBroadcast(intent);                 sendOrderedBroadcast(intent,null);             }         }        );     }
设置优先接收权限
        <receiver android:name="com.example.zhb.Com.MyBroadcastReceiver">             <intent-filter android:priority="100">                 <action android:name="com.example.broadcasttest.MY_BROADCAST"/>             </intent-filter>         </receiver>
 
截断广播继续传播
public class MyBroadcastReceiver extends BroadcastReceiver {     @Override     public void onReceive(Context context,Intent intent)     {         String value=intent.getStringExtra("key");         Toast.makeText(context,value,Toast.LENGTH_SHORT).show();         abortBroadcast();     } }
 
本地广播:之前的所有广播都是全局广播,即发出的广播可以被任何程序接收,并且也可以接受来至任何程序的广播。这样很容易引起安全性,比如数据被别的程序监听到,或者别的程序不停的发送垃圾广播。因此就有了本地广播机制。发出的广播只能在本应用程序内部传递,接收器也只能接收来自本应用程序发出的广播。本地广播是不能通过静态方法在配置文件中注册的。
public class BrocastTest extends Activity {
    private IntentFilter intentFilter;//框架的过滤器     private LocalReceiver localReceiver;//自定义的接收器     private LocalBroadcastManager localBroadcastManager;//框架的本地广播管理器
    @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.brocasttest);
        localBroadcastManager=LocalBroadcastManager.getInstance(this);//获取实例         Button btnBRCTestOne = (Button) findViewById(R.id.btnBRCTestOne);         btnBRCTestOne.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 //发送本地广播                 Intent intent1=new Intent("com.example.broadcasttest.LOCAL_BROADCAST");                 localBroadcastManager.sendBroadcast(intent1);             }         }        );
        intentFilter=new IntentFilter();         intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");         localReceiver=new LocalReceiver();         localBroadcastManager.registerReceiver(localReceiver,intentFilter);     }
    @Override     protected void onDestroy()     {         super.onDestroy();         localBroadcastManager.unregisterReceiver(localReceiver);     }
    class  LocalReceiver extends BroadcastReceiver     {         @Override         public void onReceive(Context context,Intent intent)         {             Toast.makeText(context,"接收到本地广播",Toast.LENGTH_SHORT).show();         }     } }

android入门到熟练(五)---广播

标签:

原文地址:http://www.cnblogs.com/huolongluo/p/5421531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!