Android中BroadcastReceiver(异步接收广播Intent)的使用

时间:2022-12-24 20:51:17 

Broadcast Receiver简介
Broadcast Receiver是Android的五大组件之一,使用频率也很高。
用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似.
广播 * 只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码.如:时区改变的通知,电池电量不足、用户改变了语言偏好或者开机启动等.
广播 * 没有用户界面,但是,它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.
生命周期
一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。
因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,请start service来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。
监听网络状态变化的例子
下面通过一个例子来使用BroadcastReceiver。
NetworkStateReceiver:接收网络状态变化时系统发出的Broadcast。


package com.example.networkbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
public class NetworkStateReceiver extends BroadcastReceiver {
private static final String TAG = "NetworkStateReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "network state changed.");
if (!isNetworkAvailable(context)) {
Toast.makeText(context, "network disconnected!", 0).show();
}
else Toast.makeText(context, "network connected!", 0).show();
}
/**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
}


MainActivity:


package com.example.networkbroadcastreceiver;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


静态注册和动态注册
写好BroadcastReceiver 之后要对其进行注册。
静态注册需要修改manifest文件,也是我采用的方法。
添加


<SPAN style="FONT-SIZE: 14px"><receiver android:name=".NetworkStateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver></SPAN>


动态注册的话需要这样做(未调试):
1. 在Activity的onCreate中:
//注册网络监听
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
2. 在Activity中的onDestroy中:
//取消监听
unregisterReceiver(mNetworkStateReceiver);
最终效果:
Android中BroadcastReceiver(异步接收广播Intent)的使用 

标签:BroadcastReceiver,异步
0
投稿

猜你喜欢

  • C#中的三种定时计时器Timer用法介绍

    2023-06-09 13:31:13
  • Kotlin修饰符lateinit(延迟初始化)案例详解

    2023-08-22 08:21:56
  • Android提高之MediaPlayer播放网络视频的实现方法

    2021-07-03 06:25:29
  • Android基础之隐藏标题栏/设置为全屏/横竖屏切换

    2022-06-22 14:29:35
  • Android下拉列表选项框及指示箭头动画

    2022-09-28 04:08:15
  • Spring配置中transactionAttributes的使用方法介绍

    2021-06-16 10:59:05
  • Android自定义个性化的Dialog示例

    2022-01-21 12:41:36
  • JAVA代码实现MongoDB动态条件之分页查询

    2022-05-21 12:44:11
  • 使用JDBC实现数据访问对象层(DAO)代码示例

    2021-11-12 23:33:46
  • 详解如何在Flutter中获取设备标识符

    2022-12-18 12:41:26
  • 分析java中全面的单例模式多种实现方式

    2021-12-28 05:40:29
  • android事件总线EventBus3.0使用方法详解

    2022-02-17 16:30:41
  • Java中方法名称和泛型相同的用法示例

    2023-08-12 03:18:55
  • 详解Java多线程编程中线程的启动、中断或终止操作

    2023-07-04 20:32:40
  • Android主线程和子线程区别详解

    2023-12-18 17:33:38
  • C#快速排序算法实例分析

    2023-06-17 00:30:00
  • Netty分布式FastThreadLocal的set方法实现逻辑剖析

    2021-08-22 04:51:54
  • 浅析Spring工厂的反射和配置文件

    2023-06-22 20:52:23
  • SpringBoot依赖管理的源码解析

    2022-10-29 23:37:14
  • Android实现点汇聚成字的动态效果详解

    2023-10-08 08:50:26
  • asp之家 软件编程 m.aspxhome.com