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; } } 最后在清单文件中添加了两个重要的权限:附件:http://down.51cto.com/data/2365604总结下基本有这么几个要注意的点: 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、该广播被注册之后,只要无线信号已发送改变,就会发送广播 然后 我们做出对应的判断就可以了
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:Android监听网络状态-创新互联
浏览地址:http://ybzwz.com/article/jcjeh.html