Quantcast
Channel: Android*
Viewing all articles
Browse latest Browse all 531

Android 开发之浅谈广播的运用

$
0
0

之前讲过了activity,服务service和内容提供者,四大组件就还差广播没讲,所以就顺便讲讲吧。当然,这里都是很基础的讲解,没有深入,要是深入的话光一个activity就可以讲很久。所以这里只做基础使用的讲解了。

时间紧迫,直接上代码吧。

首先,广播有两种注册方式,一种在清单文件中注册,注册后程序一运行广播就开始监听。一种在代码中注册,根据需求注册注销广播。

我们先看广播的第一种注册方式,首先定义一个广播接受者

 

[java] view plaincopy
 
  1. package com.batways.apopo.receiver;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. /** 
  8.  * @ClassName: TestReciver 
  9.  * @author victor_freedom (x_freedom_reddevil@126.com) 
  10.  * @createddate 2015年2月28日 下午9:31:23 
  11.  * @Description: TODO 
  12.  *  
  13.  */  
  14. public class TestReciver extends BroadcastReceiver {  
  15.   
  16.     @Override  
  17.     public void onReceive(Context arg0, Intent arg1) {  
  18.         // TODO Auto-generated method stub  
  19.   
  20.     }  
  21.   
  22. }  

然后再清单文件中注册

 

 

[java] view plaincopy
 
  1. <receiver android:name="com.batways.apopo.receiver.TestReciver" >  
  2.           <intent-filter>  
  3.               <action android:name="aaa.bbb.ccc" />  
  4.   
  5.               <category android:name="android.intent.category.DEFAULT" />  
  6.           </intent-filter>  
  7.       </receiver>  

这样,当程序启动的时候,广播就开始监听动作为“aaa.bbb.ccc”的意图。

 

 

接下来再看在代码中注册广播

首先在代码中new一个广播接受者

 

[java] view plaincopy
 
  1. private class UpdateCreditsBroadCast extends BroadcastReceiver {  
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.           
  5.     }  
  6.   
  7. }  

然后用代码注册:category可以填可以不填

 

 

[java] view plaincopy
 
  1. mUpdateCreditsBroadCast = new UpdateCreditsBroadCast();  
  2.         IntentFilter filter = new IntentFilter("com.batways.apopo_core.service");  
  3.         //filter.addCategory(Intent.ACTION_DEFAULT);  
  4.         registerReceiver(mUpdateCreditsBroadCast, filter);  


一般而言,在该注册文件的生命周期函数中的起始函数注册广播,在结束函数中注销广播:

 

 

[java] view plaincopy
 
  1. unregisterReceiver(mUpdateCreditsBroadCast);  
  2.         mUpdateCreditsBroadCast = null;  


这样,就完成了再代码中广播的注册与注销。

 

那么,我们发送广播的时候,该怎么弄呢。这里博主提供一个广播工具类吧,一般的都能满足使用了。这里需要特别注意,如果广播注册的时候加了category,这里就需要加,如果没有,这里就不需要加了。

 

[java] view plaincopy
 
  1. public class BroadcastHelper {  
  2.   
  3.       
  4.     /** 
  5.      * 发送String 类型的值的广播 
  6.      * @param context 
  7.      * @param action 
  8.      * @param key 
  9.      * @param value 
  10.      */  
  11.     public static void sendBroadCast(Context context,String action,String key,String value) {  
  12.         Intent intent = new Intent();  
  13.         intent.setAction(action);  
  14.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  15.         intent.putExtra(key, value);  
  16.         context.sendBroadcast(intent);  
  17.     }  
  18.       
  19.     public static void sendBroadCast(Context context,String action,String key,int value) {  
  20.         Intent intent = new Intent();  
  21.         intent.setAction(action);  
  22.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  23.         intent.putExtra(key, value);  
  24.         context.sendBroadcast(intent);  
  25.     }  
  26.       
  27.   
  28. }  
  • 广播
  • Icon Image: 

  • Technical Article
  • Android* Development Tools
  • Message Passing Interface
  • Java*
  • Android*
  • Developers
  • Professors
  • Students
  • Android*
  • Theme Zone: 

    Android
  • Android*
  • Include in RSS: 

    1
  • Beginner

  • Viewing all articles
    Browse latest Browse all 531


    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>