美文网首页
获取手机网络以及配置信息

获取手机网络以及配置信息

作者: misLiu | 来源:发表于2017-06-07 14:36 被阅读0次

    获取手机公网ip

    @SuppressLint("HandlerLeak")

    public static Handler handler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

    Bundle data = msg.getData();

    s = data.getString("s");

    setS(s);

    }

    };

    public static String getS() {

    return s;

    }

    public static void setS(String s) {

    AndroidIp.s = s;

    }

    private static String s;

    public static String GetNetIp() {

    URL infoUrl = null;

    InputStream inStream = null;

    String ipLine = "";

    HttpURLConnection httpConnection = null;

    try {

    infoUrl = new URL("http://city.ip138.com/ip2city.asp");

    URLConnection connection = infoUrl.openConnection();

    httpConnection = (HttpURLConnection) connection;

    int responseCode = httpConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) {

    inStream = httpConnection.getInputStream();

    BufferedReader reader = new BufferedReader(

    new InputStreamReader(inStream, "utf-8"));

    StringBuilder strber = new StringBuilder();

    String line = null;

    while ((line = reader.readLine()) != null)

    strber.append(line + "\n");

    Pattern pattern = Pattern

    .compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");

    Matcher matcher = pattern.matcher(strber.toString());

    if (matcher.find()) {

    ipLine = matcher.group();

    }

    }

    } catch (MalformedURLException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    try {

    if (inStream!=null){

    inStream.close();

    }

    httpConnection.disconnect();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    return ipLine;

    }

    //获取手机运营商

    public static  int getOperators(Context context) {

    // 移动设备网络代码(英语:Mobile Network Code,MNC)是与移动设备国家代码(Mobile Country Code,MCC)(也称为“MCC /

    // MNC”)相结合, 例如46000,前三位是MCC,后两位是MNC 获取手机服务商信息

    int OperatorsName = 0;

    TelephonyManager IMSI =  (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    String subscriberId = IMSI.getSubscriberId();

    // IMSI号前面3位460是国家,紧接着后面2位00 运营商代码

    System.out.println(IMSI);

    if (subscriberId.startsWith("46000") || subscriberId.startsWith("46002") || subscriberId.startsWith("46007")) {

    OperatorsName = 1;

    } else if (subscriberId.startsWith("46001") || subscriberId.startsWith("46006")) {

    OperatorsName = 2;

    } else if (subscriberId.startsWith("46003") || subscriberId.startsWith("46005")) {

    OperatorsName = 3;

    }

    return OperatorsName;

    }

    /**

    * @param context Context

    * @return true 表示网络可用

    */

    public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager connectivity = (ConnectivityManager) context

    .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity != null) {

    NetworkInfo info = connectivity.getActiveNetworkInfo();

    if (info != null && info.isConnected())

    {

    // 当前网络是连接的

    if (info.getState() == NetworkInfo.State.CONNECTED)

    {

    // 当前所连接的网络可用

    return true;

    }

    }

    }

    return false;

    }

    /**

    * 判断WIFI网络是否可用

    *

    * @param context

    * @return

    /

    public static boolean isWifiConnected(Context context) {

    if (context != null) {

    // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // 获取NetworkInfo对象

    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    //判断NetworkInfo对象是否为空 并且类型是否为WIFI

    if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)

    return networkInfo.isAvailable();

    }        return false;

    }

    /**

    * 判断MOBILE网络是否可用

    *

    * @param context

    * @return

    */

    public static boolean isMobileConnected(Context context) {

    if (context != null) {

    //获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    //获取NetworkInfo对象

    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    //判断NetworkInfo对象是否为空 并且类型是否为MOBILE

    if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)

    return networkInfo.isAvailable();

    }

    return false;

    }

    /**

    * 获取当前网络连接的类型信息

    * 原生

    *

    * @param context

    * @return

    */    public static int getConnectedType(Context context) {

    if (context != null) {

    //获取手机所有连接管理对象

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    //获取NetworkInfo对象

    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isAvailable()) {

    //返回NetworkInfo的类型

    return networkInfo.getType();

    }

    }

    return -1;

    }

    /**

    * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2

    * 自定义

    *

    * @param context

    * @return

    */

    public static int getAPNType(Context context) {

    //结果返回值

    int netType = 0;

    //获取手机所有连接管理对象

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    //获取NetworkInfo对象

    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    //NetworkInfo对象为空 则代表没有网络

    if (networkInfo == null) {

    return netType;

    }

    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型

    int nType = networkInfo.getType();

    if (nType == ConnectivityManager.TYPE_WIFI) {

    //WIFI

    netType = 1;

    } else if (nType == ConnectivityManager.TYPE_MOBILE) {

    int nSubType = networkInfo.getSubtype();

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    //3G  联通的3G为UMTS或HSDPA 电信的3G为EVDO

    if (nSubType == TelephonyManager.NETWORK_TYPE_LTE

    && !telephonyManager.isNetworkRoaming()) {

    netType = 4;

    } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS

    || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA

    || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0

    && !telephonyManager.isNetworkRoaming()) {

    netType = 3;

    //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA

    } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS

    || nSubType == TelephonyManager.NETWORK_TYPE_EDGE

    || nSubType == TelephonyManager.NETWORK_TYPE_CDMA

    && !telephonyManager.isNetworkRoaming()) {

    netType = 2;

    } else {

    netType = 2;

    }

    }

    return netType;

    }

    /**

    * 判断GPS是否打开

    *ACCESS_FINE_LOCATION权限

    * @param context

    * @return

    */

    public static boolean isGPSEnabled(Context context) {

    //获取手机所有连接LOCATION_SERVICE对象

    LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));

    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    }

    相关文章

      网友评论

          本文标题:获取手机网络以及配置信息

          本文链接:https://www.haomeiwen.com/subject/fwaofxtx.html