Android监听网络状态
该笔记是用来实时获取网络的状态,有点想QQ的断网提醒,为了以后的工作中用到,就在此处写下关于此判断的代码,下面就是写的一些代码:
成都创新互联是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的十多年时间我们累计服务了上千家以及全国政企客户,如成都封阳台等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致赞美。
package com.example.nettest; import receiver.NetReceiver; import android.app.Activity; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.Bundle; public class MainActivity extends Activity { NetReceiver mReceiver = new NetReceiver(); IntentFilter mFilter = new IntentFilter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mReceiver, mFilter); } @Override protected void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); } } package receiver; import network.NetUtils; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.widget.Toast; public class NetReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { boolean isConnected = 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(); } } } } package network; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetUtils { // 判断网络连接状态 public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } // 判断wifi状态 public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } // 判断移动网络 public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } // 获取连接类型 public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } } 最后在清单文件中添加了两个重要的权限:总结下基本有这么几个要注意的点: 1、首先要获取对应的管理器 context.getSystemService(Context.CONNECTIVITY_SERVICE) 返回ConnectivityManager 2、获取对应的网络信息状态: NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 3、判断信息是否有效 workInfo.isAvailable() 4、该广播被注册之后,只要无线信号已发送改变,就会发送广播 然后 我们做出对应的判断就可以了
附件:http://down.51cto.com/data/2365604
当前标题:Android监听网络状态
地址分享:http://scyanting.com/article/ghogdh.html