Android中BroadcastReceiver(异步接收广播Intent)的使用
2012-12-09 18:44
1126 查看
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;
相关文章推荐
- android 使用广播接收者监听短信和拦截外拨电话
- Android开发之Intent、广播和接收
- Android中Intent详解(二)之使用Intent广播事件及Broadcast Receiver简介
- android 接收广播意图错误:java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetoot
- Android使用wifi通过UDP协议发送广播数据包给PC接收不到问题解决方法
- 关于intent之android.intent.action.USER_PRESENT的接收与使用
- Android使用IntentService执行异步任务
- android广播接收器 接收广播的Intent Action 事件类型
- BroadcastReceiver 用于异步接收广播Intent
- 【Android 开发教程】使用IntentService执行异步操作
- android.intent.action.USER_PRESENT的接收与使用
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- 关于intent之android.intent.action.USER_PRESENT的接收与使用
- Android编程-IntentService使用广播与Activity通信
- Android开发之使用Intent发送广播消息(1)
- 关于intent之android.intent.action.USER_PRESENT的接收与使用
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- android 使用广播 接收和拦截 android系统短信
- Android中Intent详解(二)之使用Intent广播事件及Broadcast Receiver简介
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因