欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android提高之BroadcastReceiver实例详解

程序员文章站 2022-05-16 12:44:20
前面几篇文章分别讨论了activity和service,本文就来讨论broastcastreceiver,broastcast是应用程序间通信的手段。broastcastr...

前面几篇文章分别讨论了activity和service,本文就来讨论broastcastreceiver,broastcast是应用程序间通信的手段。broastcastreceiver也是跟intent紧密相连的,动态/静态注册了broastcastreceiver之后,使用sendbroadcast把intent发送之后,系统会自动把符合条件的broastcastreceiver启动,这和嵌入式系统的中断类似。

本文所示实例代码主要演示了如何静态/动态注册broastcastreceiver,向系统索取电量信息,以及枚举信息的字段等功能和。

程序运行截图如下所示:

Android提高之BroadcastReceiver实例详解

Android提高之BroadcastReceiver实例详解

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

Android提高之BroadcastReceiver实例详解

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

<receiver android:name="clsreceiver2">
 <intent-filter>
 <action
  android:name="com.testbroadcastreceiver.internal_2"/>
 </intent-filter>
</receiver>

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

Android提高之BroadcastReceiver实例详解

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

//当未知intent包含的内容,则需要通过以下方法来列举
bundle b=intent.getextras();
object[] lstname=b.keyset().toarray();

for(int i=0;i<lstname.length;i++)
{
 string keyname=lstname[i].tostring();
 log.e(keyname,string.valueof(b.get(keyname)));
}

main.xml的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <button android:id="@+id/button01" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="发送至内部动态注册的broadcastreceiver"></button>
 <button android:id="@+id/button02" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="发送至内部静态注册broadcastreceiver"></button>
 <button android:id="@+id/button03" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="发送至系统broadcastreceiver"></button>
</linearlayout>

testbroadcastreceiver.java的代码如下:

package com.testbroadcastreceiver;

import android.app.activity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.toast;

public class testbroadcastreceiver extends activity {
  button btninternal1,btninternal2,btnsystem;
  static final string intenal_action_1 = "com.testbroadcastreceiver.internal_1";
  static final string intenal_action_2 = "com.testbroadcastreceiver.internal_2";
  static final string intenal_action_3 = "com.testbroadcastreceiver.internal_3";
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    btninternal1=(button)this.findviewbyid(r.id.button01);
    btninternal1.setonclicklistener(new clickevent());
    btninternal2=(button)this.findviewbyid(r.id.button02);
    btninternal2.setonclicklistener(new clickevent());
    btnsystem=(button)this.findviewbyid(r.id.button03);
    btnsystem.setonclicklistener(new clickevent());
    //动态注册广播消息
 registerreceiver(bcrintenal1, new intentfilter(intenal_action_1));
  }
  class clickevent implements view.onclicklistener{

 @override
 public void onclick(view v) {
  if(v==btninternal1)//给动态注册的broadcastreceiver发送数据
  {
  intent intent = new intent(intenal_action_1);
  sendbroadcast(intent);
  }
  else if(v==btninternal2)//给静态注册的broadcastreceiver发送数据
  {
  intent intent = new intent(intenal_action_2);
  sendbroadcast(intent);
  }
  else if(v==btnsystem)//动态注册 接收2组信息的broadcastreceiver
  {
  intentfilter filter = new intentfilter();//
     filter.addaction(intent.action_battery_changed);//系统电量检测信息
     filter.addaction(intenal_action_3);//第三组自定义消息
  registerreceiver(batinforeceiver, filter);
  
  intent intent = new intent(intenal_action_3);
  intent.putextra("name", "hellogv");
  intent.putextra("blog", "http://blog.csdn.net/hellogv");
  sendbroadcast(intent);//传递过去
  }
 }
   
  }
  /*
   * 接收动态注册广播的broadcastreceiver
   */
 private broadcastreceiver bcrintenal1 = new broadcastreceiver() {
 
 public void onreceive(context context, intent intent) {
  string action = intent.getaction();
  toast.maketext(context, "动态:"+action, 1000).show();
 }
 };
 private broadcastreceiver batinforeceiver = new broadcastreceiver() {
 public void onreceive(context context, intent intent) {
  string action = intent.getaction();
  //如果捕捉到的action是action_battery_changed
  if (intent.action_battery_changed.equals(action)) {
  //当未知intent包含的内容,则需要通过以下方法来列举
  bundle b=intent.getextras();
  object[] lstname=b.keyset().toarray();

  for(int i=0;i<lstname.length;i++)
  {
   string keyname=lstname[i].tostring();
   log.e(keyname,string.valueof(b.get(keyname)));
  }
  }
  //如果捕捉到的action是intenal_action_3
  if (intenal_action_3.equals(action)) {
  //当未知intent包含的内容,则需要通过以下方法来列举
  bundle b=intent.getextras();
  object[] lstname=b.keyset().toarray();
  for(int i=0;i<lstname.length;i++)
  {
   string keyname=lstname[i].tostring();
   log.e(keyname,b.getstring(keyname));
  }
  }
 }
 };
}

clsreceiver2.java的代码如下:

package com.testbroadcastreceiver;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.widget.toast;
/*
 * 接收静态注册广播的broadcastreceiver,
 * step1:要到androidmanifest.xml这里注册消息
 *  <receiver android:name="clsreceiver2">
  <intent-filter>
  <action
   android:name="com.testbroadcastreceiver.internal_2"/>
  </intent-filter>
 </receiver>
 step2:定义消息的字符串
 step3:通过intent传递消息来驱使broadcastreceiver触发
 */
public class clsreceiver2 extends broadcastreceiver{
 @override
 public void onreceive(context context, intent intent) {
 string action = intent.getaction();
 toast.maketext(context, "静态:"+action, 1000).show();
 }
}

感兴趣的朋友可以调试运行该实例,希望能够对大家的android项目开发起到一点帮助作用。