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

Android中BroadcastReceiver(异步接收广播Intent)的使用,第1张

概述BroadcastReceiver简介BroadcastReceiver是Android的五大组件之一,使用频率也很高。用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播 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 ><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)的使用全部内容,希望文章能够帮你解决Android中BroadcastReceiver(异步接收广播Intent)的使用所遇到的程序开发问题。

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

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

原文地址: http://www.outofmemory.cn/web/1141635.html

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

发表评论

登录后才能评论

评论列表(0条)

保存