Android开发之BroadcastReceiver用法实例分析

Android开发之BroadcastReceiver用法实例分析,第1张

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

本文实例讲述了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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android开发之BroadcastReceiver用法实例分析全部内容,希望文章能够帮你解决Android开发之BroadcastReceiver用法实例分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1141954.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存