惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Webroot Blog
Webroot Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
博客园 - 司徒正美
H
Hacker News: Front Page
I
InfoQ
A
Arctic Wolf
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Heimdal Security Blog
L
LINUX DO - 最新话题
T
Threat Research - Cisco Blogs
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Security Latest
Security Latest
D
DataBreaches.Net
C
Check Point Blog
J
Java Code Geeks
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
Lohrmann on Cybersecurity
雷峰网
雷峰网
Vercel News
Vercel News
WordPress大学
WordPress大学
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy
Forbes - Security
Forbes - Security
阮一峰的网络日志
阮一峰的网络日志
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
I
Intezer
N
News and Events Feed by Topic
小众软件
小众软件
B
Blog RSS Feed
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
美团技术团队
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(二十九)------之BroadcastReceiver (转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       前面分别讨论了ActivityService,这次就轮到BroastcastReceiver,Broastcast是应用程序间通信的手段。BroastcastReceiver也是跟Intent紧密相连的,动态/静态注册了BroastcastReceiver之后,使用sendBroadcast把Intent发送之后,系统会自动把符合条件的BroastcastReceiver启动,跟嵌入式系统的中断类似。

        本文主要演示了如何静态/动态注册BroastcastReceiver,向系统索取电量信息,以及枚举信息的字段。本文运行截图如下:

上图是发送Intent至内部动态注册的BroadcastReceiver,接收到之后显示消息名称。动态注册BroadcastReceiver用到registerReceiver()。

上图是发送Intent至内部静态注册的BroadcastReceiver,接收到之后显示消息名称。静态注册比动态注册麻烦点,先新建一个类继承BroadcastReceiver,然后到AndroidManifest.xml 添加

  1. <receiver android:name="clsReceiver2">  
  2.     <intent-filter>  
  3.         <action  
  4.             android:name="com.testBroadcastReceiver.Internal_2"/>  
  5.     </intent-filter>  
  6. </receiver>  

第一个name是类名,第二个是action的名称。

上图是枚举Intent消息的字段,这个功能比较适合懒人,把收到的Intent消息的字段全部分解了,再看看哪个需要的,懒得记住。实现这部分的代码如下:

  1.   
  2.                 Bundle b=intent.getExtras();  
  3.                 Object[] lstName=b.keySet().toArray();  
  4.   
  5.                 for(int i=0;i<lstName.length;i++)  
  6.                 {  
  7.                     String keyName=lstName[i].toString();  
  8.                     Log.e(keyName,String.valueOf(b.get(keyName)));  
  9.                 }  

main.xml的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <Button android:id="@+id/Button01" android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content" android:text="发送至内部动态注册的BroadcastReceiver"></Button>  
  8.     <Button android:id="@+id/Button02" android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" android:text="发送至内部静态注册BroadcastReceiver"></Button>  
  10.     <Button android:id="@+id/Button03" android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" android:text="发送至系统BroadcastReceiver"></Button>  
  12. </LinearLayout>  

testBroadcastReceiver.java的代码如下:

  1. package com.testBroadcastReceiver;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.Toast;  
  13.   
  14. public class testBroadcastReceiver extends Activity {  
  15.     Button btnInternal1,btnInternal2,btnSystem;  
  16.     static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1";  
  17.     static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2";  
  18.     static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3";  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         btnInternal1=(Button)this.findViewById(R.id.Button01);  
  24.         btnInternal1.setOnClickListener(new ClickEvent());  
  25.         btnInternal2=(Button)this.findViewById(R.id.Button02);  
  26.         btnInternal2.setOnClickListener(new ClickEvent());  
  27.         btnSystem=(Button)this.findViewById(R.id.Button03);  
  28.         btnSystem.setOnClickListener(new ClickEvent());  
  29.           
  30.         registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1));  
  31.     }  
  32.     class ClickEvent implements View.OnClickListener{  
  33.   
  34.         @Override  
  35.         public void onClick(View v) {  
  36.             if(v==btnInternal1)  
  37.             {  
  38.                 Intent intent = new Intent(INTENAL_ACTION_1);  
  39.                 sendBroadcast(intent);  
  40.             }  
  41.             else if(v==btnInternal2)  
  42.             {  
  43.                 Intent intent = new Intent(INTENAL_ACTION_2);  
  44.                 sendBroadcast(intent);  
  45.             }  
  46.             else if(v==btnSystem)  
  47.             {  
  48.                 IntentFilter filter = new IntentFilter();  
  49.                 filter.addAction(Intent.ACTION_BATTERY_CHANGED);  
  50.                 filter.addAction(INTENAL_ACTION_3);  
  51.                 registerReceiver(batInfoReceiver, filter);  
  52.                   
  53.                 Intent intent = new Intent(INTENAL_ACTION_3);  
  54.                 intent.putExtra("Name""hellogv");  
  55.                 intent.putExtra("Blog""http://blog.csdn.net/hellogv");  
  56.                 sendBroadcast(intent);  
  57.             }  
  58.         }  
  59.           
  60.     }  
  61.       
  62.      
  63.  
  64.   
  65.     private BroadcastReceiver bcrIntenal1 = new BroadcastReceiver() {  
  66.           
  67.         public void onReceive(Context context, Intent intent) {  
  68.             String action = intent.getAction();  
  69.             Toast.makeText(context, "动态:"+action, 1000).show();  
  70.         }  
  71.     };  
  72.       
  73.   
  74.     private BroadcastReceiver batInfoReceiver = new BroadcastReceiver() {  
  75.           
  76.         public void onReceive(Context context, Intent intent) {  
  77.             String action = intent.getAction();  
  78.               
  79.             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {  
  80.                   
  81.                 Bundle b=intent.getExtras();  
  82.                 Object[] lstName=b.keySet().toArray();  
  83.   
  84.                 for(int i=0;i<lstName.length;i++)  
  85.                 {  
  86.                     String keyName=lstName[i].toString();  
  87.                     Log.e(keyName,String.valueOf(b.get(keyName)));  
  88.                 }  
  89.             }  
  90.               
  91.             if (INTENAL_ACTION_3.equals(action)) {  
  92.                   
  93.                 Bundle b=intent.getExtras();  
  94.                 Object[] lstName=b.keySet().toArray();  
  95.   
  96.                 for(int i=0;i<lstName.length;i++)  
  97.                 {  
  98.                     String keyName=lstName[i].toString();  
  99.                     Log.e(keyName,b.getString(keyName));  
  100.                 }  
  101.             }  
  102.         }  
  103.     };  
  104.   
  105.   
  106. }  

clsReceiver2.java的代码如下:

  1. package com.testBroadcastReceiver;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   
  20. public class clsReceiver2 extends BroadcastReceiver{  
  21.     @Override  
  22.     public void onReceive(Context context, Intent intent) {  
  23.         String action = intent.getAction();  
  24.         Toast.makeText(context, "静态:"+action, 1000).show();  
  25.           
  26.     }  
  27. }