该笔记是用来实时获取网络的状态,有点想QQ的断网提醒,为了以后的工作中用到,就在此处写下关于此判断的代码,下面就是写的一些代码:

packagecom.example.nettest;importreceiver.NetReceiver;importandroid.app.Activity;importandroid.content.IntentFilter;importandroid.net.ConnectivityManager;importandroid.os.Bundle;publicclassMainActivityextendsActivity{NetReceivermReceiver=newNetReceiver();IntentFiltermFilter=newIntentFilter();@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);registerReceiver(mReceiver,mFilter);}@OverrideprotectedvoidonDestroy(){unregisterReceiver(mReceiver);super.onDestroy();}}packagereceiver;importnetwork.NetUtils;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.net.ConnectivityManager;importandroid.widget.Toast;publicclassNetReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){booleanisConnected=NetUtils.isNetworkConnected(context);System.out.println("网络状态:"+isConnected);System.out.println("wifi状态:"+NetUtils.isWifiConnected(context));System.out.println("移动网络状态:"+NetUtils.isMobileConnected(context));System.out.println("网络连接类型:"+NetUtils.getConnectedType(context));if(isConnected){Toast.makeText(context,"已经连接网络",Toast.LENGTH_LONG).show();}else{Toast.makeText(context,"已经断开网络",Toast.LENGTH_LONG).show();}}}}packagenetwork;importandroid.content.Context;importandroid.net.ConnectivityManager;importandroid.net.NetworkInfo;publicclassNetUtils{//判断网络连接状态publicstaticbooleanisNetworkConnected(Contextcontext){if(context!=null){ConnectivityManagermConnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfomNetworkInfo=mConnectivityManager.getActiveNetworkInfo();if(mNetworkInfo!=null){returnmNetworkInfo.isAvailable();}}returnfalse;}//判断wifi状态publicstaticbooleanisWifiConnected(Contextcontext){if(context!=null){ConnectivityManagermConnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfomWiFiNetworkInfo=mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);if(mWiFiNetworkInfo!=null){returnmWiFiNetworkInfo.isAvailable();}}returnfalse;}//判断移动网络publicstaticbooleanisMobileConnected(Contextcontext){if(context!=null){ConnectivityManagermConnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfomMobileNetworkInfo=mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if(mMobileNetworkInfo!=null){returnmMobileNetworkInfo.isAvailable();}}returnfalse;}//获取连接类型publicstaticintgetConnectedType(Contextcontext){if(context!=null){ConnectivityManagermConnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfomNetworkInfo=mConnectivityManager.getActiveNetworkInfo();if(mNetworkInfo!=null&&mNetworkInfo.isAvailable()){returnmNetworkInfo.getType();}}return-1;}}最后在清单文件中添加了两个重要的权限:<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permissionandroid:name="android.permission.INTERNET"/>总结下基本有这么几个要注意的点:1、首先要获取对应的管理器context.getSystemService(Context.CONNECTIVITY_SERVICE)返回ConnectivityManager2、获取对应的网络信息状态:NetworkInfomNetworkInfo=mConnectivityManager.getActiveNetworkInfo();NetworkInfomWiFiNetworkInfo=mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);NetworkInfomMobileNetworkInfo=mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);3、判断信息是否有效workInfo.isAvailable()4、该广播被注册之后,只要无线信号已发送改变,就会发送广播然后我们做出对应的判断就可以了



附件:http://down.51cto.com/data/2365604