您好!欢迎来到源码码网

Android BroadcastReceiver:接收广播

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-13 17:49
  • 阅读:466

广播(Broadcast)是 Android 系统中应用程序间通信的手段。

当有特定事件发生时,例如有来电、有短信、电池电量变化等事件发生时,Android 系统都会产生特定的 Intent 对象并且自动进行广播,而针对特定事件注册的 BroadcastReceiver 会接收到这些广播,并获取 Intent 对象中的数据进行处理。

在广播 Intent对象时可以指定用户权限,以此限制仅有获得了相应权限的 BroadcastReceiver 才能接收并处理对应的广播。

BroadcastReceiver 有动态和静态两种注册方法。

  • 动态注册方法即使用 Context. registerReceiver() 方法进行注册,需要特别注意的是,动态注册方法在退出程序前要使用 Context.unregisterReceiver() 方法撤销注册。

  • 静态注册方法即在 AndroidManifest.xml 文件中通过 <receiver> 标签进行注册。


一个 BroadcastReceiver 对象只有在被调用 onReceive(Context, Intent) 时才有效,当从该函数返回后,该对象就已无效了,其生命周期结束。

下面介绍如何使用动态注册来实现监听电池剩余电量。

实例 BatteryDemo 演示了使用动态注册 BroadcastReceiver 对象并且接收系统电量改变事件并加以处理的过程,运行效果如图 1 所示。

BatteryDemo的运行效果

图 1  BatteryDemo的运行效果

实例 BatteryDemo 中 main.xml 的代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">


  6. <ToggleButton

  7. android:id="@+id/button"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:textOff="停止检测"

  11. android:textOn="检测当前手机电量" />


  12. <TextView

  13. android:id="@+id/text"

  14. android:layout_width="fill_parent"

  15. android:layout_height="wrap_content" />

  16. </LinearLayout>

实例 BatteryDemo 中 AndroidManifest.xml 的代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.batterydemo"

  4. android:versionCode="1"

  5. android:versionName="1.0">


  6. <uses-sdk android:minSdkVersion="14" />

  7. <application

  8. android:allowBackup="true"

  9. android:icon="@mipmap/ic_launcher"

  10. android:label="@string/app_name"

  11. android:roundIcon="@mipmap/ic_launcher_round"

  12. android:supportsRtl="true"

  13. android:theme="@style/AppTheme">

  14. <activity android:name=".MainActivity">

  15. <intent-filter>

  16. <action android:name="android.intent.action.MAIN" />

  17. <category android:name="android.intent.category.LAUNCHER" />

  18. </intent-filter>

  19. </activity>

  20. </application>


  21. </manifest>

实例 BatteryDemo 中 MainActivity.java 的具体实现代码如下:


  1. package introduction.android.batterydemo;


  2. import android.content.BroadcastReceiver;

  3. import android.content.Context;

  4. import android.content.Intent;

  5. import android.content.IntentFilter;

  6. import android.support.v7.app.AppCompatActivity;

  7. import android.os.Bundle;

  8. import android.widget.CompoundButton;

  9. import android.widget.TextView;

  10. import android.widget.ToggleButton;


  11. import org.w3c.dom.Text;


  12. public class MainActivity extends AppCompatActivity {

  13. private ToggleButton button;

  14. private TextView text;

  15. BroadcastReceiver receiver = null;


  16. @Override

  17. protected void onCreate(Bundle savedInstanceState) {

  18. super.onCreate(savedInstanceState);

  19. setContentView(R.layout.activity_main);

  20. button = (ToggleButton) findViewById(R.id.button);

  21. text = (TextView) findViewById(R.id.text);


  22. final BroadcastReceiver receiver = new BroadcastReceiver() {

  23. @Override

  24. public void onReceive(Context context, Intent intent) {

  25. String action = intent.getAction();

  26. if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {

  27. int current = intent.getExtras().getInt("level");

  28. int total = intent.getExtras().getInt("scale");

  29. int value = current * 100 / total;

  30. text.setText("当前电量是" + value + "%" + "");

  31. }

  32. }

  33. };

  34. button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

  35. @Override

  36. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

  37. if (isChecked) {

  38. IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

  39. registerReceiver(receiver, filter);

  40. } else {

  41. unregisterReceiver(receiver);

  42. text.setText("");


  43. }

  44. }

  45. });

  46. }

  47. }

其中,Intent.ACTION_BATTERY_CHANGED 为当电池电量变化时产生的 Intent 对象中携带的 Action 信息。

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED) ;

用于确定当前 BroadcastReceiver 对象接收的 Intent 对象的类型。

下面是常用的方法:

  • registerReceiver(receiver, filter): 动态注册 receiver。

  • int current = intent.getExtras().getInt("level"): 获取当前电池的电量。

  • int total=intent.getExtras().getInt("scale"): 获取总电量。

  • unregisterReceiver(receiver):注销 receiver 注册。


该应用程序若要使用静态注册,则需要在 AndroidManifest.xml 文件中添加如下代码:

  1. <receiver android:name="receiver">

  2. <intent-filter>

  3. <action android:name="android.intent.action.BATTERY_CHANGED"/>

  4. </intent-filter>

  5. </receiver>


特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • bootstrap ui框架能用在uniapp中吗?
  • bootstrap ui框架能用在uniapp中吗?
  • BootstrapUI框架通常是前端开发中的一种工具,它提供了一套预定义的CSS样式和组件,用于快速构建响应式布局的网页。然而,UniApp是一个使用Vue.js开发跨平台应用的框架,它可以用来开发iOS、Android、以及各种小程序和H5应用。
  • 互动社区
  • 来源:源码码网
  • 编辑:热度建站
  • 时间:2024-04-12 00:04
  • 阅读:135
  • css实现banner图由中心点动态放大效果
  • css实现banner图由中心点动态放大效果
  • 在日常的网页设计中,为了让网页增加一定的特效以达到交互的目的,我们尝尝会在网页中使用一些动画效果。今天来说说实现banner图由中心点动态放大效果,实现这个效果需要用到css中的动画:animation​和关键帧:@keyframes,具体示例如下:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-11 18:52
  • 阅读:150
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • 在网页开发中为了提升网页的交互效果,经常会用到使用js给网页增加一定的特效,下边就来说说使用js实现鼠标滑动到某个位置数字自动滚动增加的效果。其实这种效果有很多中解决办法,自己也可以去写,下边我们借助countUp.js来实现,关于这个js文件,我放在末尾:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-08 09:20
  • 阅读:227
  • 响应式网页设计思路及注意事项
  • 响应式网页设计思路及注意事项
  • 一、什么是响应式网页响应式网页设计就是让网页具有根据设备类型应用CSS样式的能力。设计:设想、计划。设计就是把想法实现。网页设计:按照一定的设计思路布局网页内容。传统网页设计:都是针对PC端浏览器而设计的,不具备查询设备的能力,更不能对多种访问设备做出响应。传统网页设计的弊端:在移动互联网时代,传统的网页设计不适合多屏幕时代。响应式网页设计应运而生。响应式网页设计是一种设计网页的思想/方法。响应:指让我们的网页能够自动查询用户的访问设备
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-02 11:24
  • 阅读:130
  • css中rel的属性值都有哪些,分别代表什么意思
  • css中rel的属性值都有哪些,分别代表什么意思
  • 在HTML中,元素的rel属性用于定义当前文档与被链接文档之间的关系。这个属性在CSS的上下文中经常与样式表关联,但rel属性的用途远不止于此。以下是一些常见的rel属性值及其意义:1、stylesheet:表示被链接的文档是一个样式表。这通常用于链接CSS文件。
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-03-28 12:28
  • 阅读:264
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部