Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發中ConnectivityManager應用

Android開發中ConnectivityManager應用

編輯:關於android開發

  /**
           * 測試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));

          }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved