• Android开发中ConnectivityManager应用


    /**
               * 测试ConnectivityManager ConnectivityManager主要管理和网络连接相关的操作
               * 相关的TelephonyManager则管理和手机、运营商等的相关信息;WifiManager则管理和wifi相关的信息。
               * 想访问网络状态,首先得添加权限<uses-permission
               * android:name="android.permission.ACCESS_NETWORK_STATE"/>
               * NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,通过其getState()方法获取的State对象则代表着
               * 连接成功与否等状态。
               *
               */
              public void testConnectivityManager()
              {
                      ConnectivityManager connManager = (ConnectivityManager) this
                                      .getSystemService(CONNECTIVITY_SERVICE);
                      // 获取代表联网状态的NetWorkInfo对象
                      NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
                      // 获取当前的网络连接是否可用
                      if (null == networkInfo)
                      {
                              Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
                              //当网络不可用时,跳转到网络设置页面
                              startActivityForResult(new Intent(
                                              android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1);

                      } else
                      {
                              boolean available = networkInfo.isAvailable();
                              if (available)
                              {
                                      Log.i("通知", "当前的网络连接可用");
                                      Toast.makeText(this, "当前的网络连接可用", Toast.LENGTH_SHORT).show();
                              } else
                              {
                                      Log.i("通知", "当前的网络连接不可用");
                                      Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
                              }
                      }

                      State state = connManager.getNetworkInfo(
                                      ConnectivityManager.TYPE_MOBILE).getState();
                      if (State.CONNECTED == state)
                      {
                              Log.i("通知", "GPRS网络已连接");
                              Toast.makeText(this, "GPRS网络已连接", Toast.LENGTH_SHORT).show();
                      }

                      state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                                      .getState();
                      if (State.CONNECTED == state)
                      {
                              Log.i("通知", "WIFI网络已连接");
                              Toast.makeText(this, "WIFI网络已连接", Toast.LENGTH_SHORT).show();
                      }

                      // // 跳转到无线网络设置界面
                      // startActivity(new
                      // Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                      // // 跳转到无限wifi网络设置界面
                      // startActivity(new
                      // Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));

    /**
         * 检查网络连接情况
         *
         * @return 0: None, 1: Wifi, 2: GPRS, 3: Other
         */
        protected int checkNetworkType() {
            // if (Global.IsDebug)
            // return 1;

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(InitActivity.CONNECTIVITY_SERVICE);
            // 获取代表联网状态的NetWorkInfo对象
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            // 获取当前的网络连接是否可用
            if (networkInfo == null || !networkInfo.isAvailable())
                return 0;

            // Wifi
            State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            if (State.CONNECTED == state) {
                return 1;
            }

            // GPRS
            state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
            if (State.CONNECTED == state) {
                return 2;
            }

            return 3;
        }

  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/jiayonghua/p/2455249.html
Copyright © 2020-2023  润新知