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;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM