android用户网络检测工具
16lz
2022-07-29
public class NetUtil{private static String PROXY = ""; // 移动运营商代理: 中国移动:10.0.0.172 其它的忘了private static int PORT = 0; // 端口:80/** * 检查用户网络 */public static boolean checkNet(Context context){// 判断WIFI连接boolean isWifi = isWifiConnection(context);// 判断Mobile连接boolean isMobile = isMobileConnection(context);// 如果Mobile连接,判断是哪个APN被选中了if(isMobile){// APN被选中,代理信息是否有内容,如果wap方式readAPN(context); // 判断是哪个APN被选中// TODO}if(!isWifi && !isMobile){return false;}return true;}/** * 读取被选中的APN * @param context */private static void readAPN(Context context) {// 操作联系人ContentResolver resolver = context.getContentResolver();Uri uri = Uri.parse("content://telephony/carriers/preferapn");Cursor cursor = resolver.query(uri, null, null, null, null);if(cursor!=null && cursor.moveToNext()){PROXY = cursor.getString(cursor.getColumnIndex("proxy"));PORT = cursor.getInt(cursor.getColumnIndex("port"));}}/** * 判断: Mobile连接 * @param context * @return */private static boolean isMobileConnection(Context context) {ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if(networkInfo!=null){return networkInfo.isConnected();}return false;}/** * 判断: Wifi连接 * @param context * @return */private static boolean isWifiConnection(Context context) {ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);if(networkInfo!=null){return networkInfo.isConnected();}return false;}}
更多相关文章
- Android(安卓)热点开关状态的判断和获取热点ssid
- Android中判断网络功能是否可用
- Android的网络状态判断
- [android]在上下文菜单的选中事件中获取列表选中的元素
- Android,LIstView中的OnItemClick点击无效的解决办法
- Android打开/关闭数据流量
- 重定向android log
- Android(安卓)PureMVC
- 【android测试】值得学习的android测试知识连接