当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发之BroadcastReceiver用法实例分析

Android开发之BroadcastReceiver用法实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android开发中broadcastreceiver用法。分享给大家供大家参考。具体分析如下:

在android系统中,广播(broadcast)是在组件之间传播数据(intent)的一种机制。

braodcast receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的。我们可以用intent来启动一个组件,也可以用sendbroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现broadcast receiver来监听和响应广播的intent。

事件的广播通过创建intent对象并调用sendbroadcast()方法将广播发出。事件的接受是通过定义一个继承broadcastreceiver的类来实现的,继承该类后覆盖其onreceive()方法,在该方法中响应事件。

下面是android系统中定义了很多标准的broadcast action来响应系统的广播事件。

①action_time_changed(时间改变时触发)
②action_boot_completed(系统启动完成后触发)--比如有些程序开机后启动就是用这种方式来实现的
③action_package_added(添加包时触发)
④action_battery_changed(电量低时触发)

下面看一个例子:

我们在一个按钮上绑定一个事件,事件通过发送一个广播来触发logcat打出一个log。

先看manifest文件。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.test" 
  android:versioncode="1" 
  android:versionname="1.0" > 
  <uses-sdk 
    android:minsdkversion="8" 
    android:targetsdkversion="16" />
  <application 
    android:allowbackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/apptheme" >
    <activity 
      android:name="com.example.broadcast.broadcastreceiveractivity" 
      android:label="@string/app_name_bc" >
      <intent-filter>
        <action android:name="android.intent.action.main" >
        </action>
        <category android:name="android.intent.category.launcher" >
        </category>
      </intent-filter>
    </activity>
    <receiver android:name="com.example.broadcast.hellobroadreciever" >
      <intent-filter>
        <action android:name="comz.test.printlog" >
        </action>
      </intent-filter>
    </receiver>
  </application>
</manifest>

上面声明了一个receiver。接收名字是comz.test.printlog的消息。

看activity:

package com.example.broadcast;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import com.example.test.r;
public class broadcastreceiveractivity extends activity {
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    button b1 = (button) findviewbyid(r.id.broadcastbtn);
    b1.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        log.e("mason", "here");
        // 定义一个intent
        intent intent = new intent().setaction("comz.test.printlog")
            .putextra("info", "here is your info.");
        // 广播出去
        sendbroadcast(intent);
      }
    });
  }
}

在这段代码中,定义一个intent并发送广播出去。

看broadreceiver的代码:

package com.example.broadcast;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.util.log;
public class hellobroadreciever extends broadcastreceiver {
  // 如果接收的事件发生
  @override
  public void onreceive(context context, intent intent) {
    log.e("mason", "on receive");
    if (intent.getaction().equals("comz.test.printlog")) {
      log.e("mason", intent.getstringextra("info"));
    }
  }
}

这是broadcastreceiver的代码。

在接收到消息之后,如果消息是comz.test.printlog,则打印消息。

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网