源码教程 2025年06月7日
0 收藏 0 点赞 377 浏览 3790 个字
摘要 :

广播(Broadcast)是 Android 系统中应用程序间通信的手段。当有特定事件发生时,例如有来电、有短信、电池电量变化等事件发生时,Android 系统都会产生特定的……

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

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

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

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

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

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


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

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

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

Android BroadcastReceiver:接收广播

图 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>

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1026.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号