Android学习之BroadcastReceiver总结

本文详细介绍了Android中的BroadcastReceiver组件,包括其概念、分类、编程流程及实例演示。通过静态和动态注册方式,实现自定义与系统广播事件的发送与接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android学习之BroadcastReceiver总结

代码下载:http://download.youkuaiyun.com/detail/nuptboyzhb/4482544

关于BroadcastReceiver的概述:
①广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播──比如说,通知其它应用程序一些数据下载完成并处于可用状态。
②应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自BroadcastReceiver基类
广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。

BroadcastReceiver事件分类

①系统广播事件,比如:ACTION_BOOT_COMPLETED(系统启动完成后触发),ACTION_TIME_CHANGED(系统时间改变时触发),ACTION_BATTERY_LOW(电量低时触发)等等。

②用户自定义的广播事件。

BroadcastReceiver事件的编程流程

①注册广播事件:注册方式有两种,一种是静态注册,就是在 AndroidManifest.xml文件中定义,注册的广播接收器必须要继承BroadcastReceiver;另一种是动态注册,是在程序中使用 Context.registerReceiver注册,注册的广播接收器相当于一个匿名类。两种方式都需要IntentFIlter

②发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。

③接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog。

BroadcastReceiver事件的编程举例

说明:该项目举例说明了4种情况的广播事件,静态注册的系统广播事件、静态注册的用户自定义广播事件、动态注册的系统广播事件和动态注册的用户自定义广播事件。

1.MainActivity.java文件内容

package com.byread;

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.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

publicclass MainActivity extends Activity {

private Button sendStaticBtn;

private Button sendDynamicBtn;

private Button sendSystemBtn;

privatestaticfinal String STATICACTION = "com.byread.static";

privatestaticfinal String DYNAMICACTION = "com.byread.dynamic";

// 系统Action:

privatestaticfinal String SYSTEMACTION = Intent.ACTION_BATTERY_CHANGED;

//重写ActivityonCreate方法

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

sendStaticBtn = (Button)findViewById(R.id.send_static);

sendDynamicBtn = (Button)findViewById(R.id.send_dynamic);

sendSystemBtn = (Button)findViewById(R.id.send_system);

sendStaticBtn.setOnClickListener(newMyOnClickListener());

sendDynamicBtn.setOnClickListener(newMyOnClickListener());

sendSystemBtn.setOnClickListener(newMyOnClickListener());

}

//内部类,用于监听按钮消息

class MyOnClickListener implements OnClickListener{

@Override

publicvoid onClick(View v) {

// 发送自定义静态注册广播消息

if(v.getId() == R.id.send_static){

Log.e("MainActivity", "发送自定义静态注册广播消息");

Intentintent = newIntent();

intent.setAction(STATICACTION);

intent.putExtra("msg", "接收静态注册广播成功!");

sendBroadcast(intent);

}

// 发送自定义动态注册广播消息

elseif(v.getId() == R.id.send_dynamic){

Log.e("MainActivity", "发送自定义动态注册广播消息");

Intentintent = newIntent();

intent.setAction(DYNAMICACTION);

intent.putExtra("msg", "接收动态注册广播成功!");

sendBroadcast(intent);

}

// 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。

elseif(v.getId() == R.id.send_system){

Log.e("MainActivity", "发送系统动态注册广播消息");

Intentintent = newIntent();

intent.setAction(SYSTEMACTION);

intent.putExtra("msg", "正在充电。。。。");

}

}

}

//重写MainActivityonStart()函数

@Override

protectedvoid onStart() {

super.onStart();

Log.e("MainActivity", "注册广播事件");

// 1 注册自定义动态广播消息

IntentFilterfilter_dynamic = new IntentFilter();

filter_dynamic.addAction(DYNAMICACTION);

registerReceiver(dynamicReceiver, filter_dynamic);

// 1 注册系统动态广播消息

IntentFilterfilter_system = new IntentFilter();

filter_system.addAction(SYSTEMACTION);

registerReceiver(systemReceiver, filter_system);

}

// 2 自定义动态广播接收器,内部类

private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {

@Override

publicvoid onReceive(Context context, Intent intent) {

Log.e("MainActivity", "接收自定义动态注册广播消息");

if(intent.getAction().equals(DYNAMICACTION)){

String msg = intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

};

// 2 系统动态广播接收器,内部类

private BroadcastReceiver systemReceiver = new BroadcastReceiver() {

@Override

publicvoid onReceive(Context context, Intent intent) {

Log.e("MainActivity", "接收系统动态注册广播消息");

if(intent.getAction().equals(SYSTEMACTION)){

String msg = intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

};

}

2.自定义静态注册广播消息接收器

package com.byread;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

/**

* 自定义静态注册广播消息接收器

* @author zuolongsnail

*

*/

publicclass StaticReceiver extends BroadcastReceiver {

@Override

publicvoid onReceive(Context context, Intent intent) {

String msg= intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

3.系统静态注册广播消息接收器

package com.byread;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

import android.widget.Toast;

/**

* 系统静态注册广播消息接收器

*

* @author zuolongsnail

*

*/

publicclass SystemReceiver extends BroadcastReceiver {

@Override

publicvoid onReceive(Context context, Intent intent) {

if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {

Log.e("SystemReceiver", "电量低提示");

Toast.makeText(context, "您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show();

}

}

}

4.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">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content"android:text="@string/hello" />

<Button android:id="@+id/send_static"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送自定义静态注册广播" />

<Button android:id="@+id/send_dynamic"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送自定义动态注册广播" />

<Button android:id="@+id/send_system"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送系统动态注册广播" />

</LinearLayout>

5. AndroidManifest.xml文件

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

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

package="com.byread"android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".MainActivity"android:label="@string/app_name">

<intent-filter>

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

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

</intent-filter>

</activity>

<!-- 注册自定义静态广播接收器-->

<receiver android:name=".StaticReceiver">

<intent-filter>

<action android:name="com.byread.static"/>

</intent-filter>

</receiver>

<!-- 注册系统静态广播接收器-->

<receiver android:name=".SystemReceiver">

<intent-filter>

<action android:name="android.intent.action.BATTERY_LOW"/>

</intent-filter>

</receiver>

</application>

</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值