android用户网络检测工具

来源:互联网 发布:一知f君张碧晨 编辑:程序博客网 时间:2024/06/05 05:55
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;}}

0 0
原创粉丝点击