Android中BroadcastReceiver使用总结

简介:

Android中BroadcastReceiver使用分为动态注册BroastcastReceiver ,静态注册BroastcastReceiver

1、动态注册BroastcastReceiver

定义BroadcastReceiver:

        private  BroadcastReceiver bcr1 = new  BroadcastReceiver() {
    @Override
    public  void  onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         String action = intent.getAction(); 
         Toast.makeText(context, "动态:" +action, 1000 ).show(); 
        
   
};

 注册registerReceiver(bcr1, new IntentFilter(ACTION_1));代码如下。关键代码是注册代码registerReceiver(bcr1, new IntentFilter(ACTION_1)),其他是一些简单的按钮, 及绑定的Click事件。

Button btn1,btn2,btn3; 
  static  final  String ACTION_1 = "com.example.broadcastreceiverdemo.ACTION_1"
  static  final  String ACTION_2 = "com.example.broadcastreceiverdemo.ACTION_2"
  static  final  String ACTION_3 = "com.example.broadcastreceiverdemo.ACTION_3"
 
@Override
protected  void  onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     btn1 = (Button) this .findViewById(R.id.button1);
     btn1.setOnClickListener( new  ClickEvent());
     
     btn2 = (Button) this .findViewById(R.id.button2);
     btn2.setOnClickListener( new  ClickEvent());
     
     btn3 = (Button) this .findViewById(R.id.button3);
     btn3.setOnClickListener( new  ClickEvent());
     
     registerReceiver(bcr1, new  IntentFilter(ACTION_1));
}

 发送广播

class  ClickEvent implements  OnClickListener {
 
   @Override
   public  void  onClick(View v) {
       // TODO Auto-generated method stub
       if (v == btn1)
       {
           Intent intent = new  Intent(ACTION_1);
           sendBroadcast(intent);
       }
       if (v == btn2)
       {
           Intent intent = new  Intent(ACTION_2);
           sendBroadcast(intent);
       }
       if (v == btn3)
       {
           IntentFilter filter = new  IntentFilter();
           filter.addAction(Intent.ACTION_BATTERY_LOW);
           filter.addAction(ACTION_3);
           
           registerReceiver(bcr2, new  IntentFilter(ACTION_3));
           
           Intent intent = new  Intent(ACTION_3);
           intent.putExtra( "Conuntry" , "China" );
           intent.putExtra( "City" , "ShangHai" );
           sendBroadcast(intent);
       }
   }

   其中 v == btn1为发送至内部动态注册的BroadcastReceiver;

       v == btn2为发送至内部静态注册的BroadcastReceiver;

   v == btn3为发送至系统级内部动态注册BroadcastReceiver(bt1和bt3基本一样,只是稍微加了addAction以及Intent的putExtra);

 

2 静态注册BroastcastReceiver。静态注册更要烦些,要在Androidmanifest.xml中注册。

<application
     android:allowBackup= "true"
     android:icon= "@drawable/ic_launcher"
     android:label= "@string/app_name"
     android:theme= "@style/AppTheme"  >
     <receiver android:name= "cls2" >
         <intent-filter>
             <action android:name= "com.example.broadcastreceiverdemo.ACTION_2" ></action>
         </intent-filter>
     </receiver>
     <activity
         android:name= "com.example.broadcastreceiverdemo.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>

 并且要实现一个类继承BroadcastReceiver

public  class  cls2 extends  BroadcastReceiver{
 
     @Override
     public  void  onReceive(Context context, Intent intent) {
         // TODO Auto-generated method stub
         String action = intent.getAction(); 
         Toast.makeText(context, "静态:" +action, 1000 ).show(); 
 
     }
}

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/14/2959367.html,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
59 0
|
存储 缓存 前端开发
关于Android SurfaceView截屏总结
关于Android SurfaceView截屏总结
1653 0
|
5月前
|
设计模式 Android开发
44. 【Android教程】广播接收器:Broadcast Receiver
44. 【Android教程】广播接收器:Broadcast Receiver
76 2
|
6月前
|
Android开发 Kotlin
android开发,使用kotlin学习BroadcastReceiver
android开发,使用kotlin学习BroadcastReceiver
221 0
|
编解码 Android开发
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
562 0
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
|
Android开发
Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信(二)
Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信
113 0
|
Android开发
Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信(一)
Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信
109 0
|
数据库 Android开发 开发者
Android 开发四大组件(Activity、Service、Broadcast Receiver、Content Provider)
Android 开发四大组件(Activity、Service、Broadcast Receiver、Content Provider)
150 0
|
API Android开发 开发者
Android四大组件-BroadcastReceiver
Android四大组件-BroadcastReceiver
114 0
|
开发工具 Android开发
Android推送集成方案总结
刚做完推送集成方案,记录下坑。 这里记录的特性和使用时针对写blog时采用的sdk的,具体使用流程和限制还请参考官方给出的sdk. #### 1、推送规则 小米手机用小米推送; 华为手机用华为推送; 其他手机用友盟推送。