该笔记是用来实时获取网络的状态,有点想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; } } 最后在清单文件中添加了两个重要的权限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> 总结下基本有这么几个要注意的点: 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、该广播被注册之后,只要无线信号已发送改变,就会发送广播 然后 我们做出对应的判断就可以了
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。