2012/07/27

Android 檢查網路狀態、GPS狀態

最近跟國中朋友聊天,他最近在寫Android的衛星定位地圖
很可惜的是一直出錯,看了一下他的Code只是少了權限而已…
所以就有這篇筆記文了

判斷GPS是否開啟:


   LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    Toast.makeText(MainActivity.this, "以開啟定位服務", Toast.LENGTH_LONG)
      .show();
   } else {
    Toast.makeText(MainActivity.this, "請開啟定位服務", Toast.LENGTH_LONG)
      .show();
   }


記得要加入下面的權限:
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

判斷網路是否開啟:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   if (wifiManager.isWifiEnabled()) {
    Log.e("開啟", "wifi");
   } else {
    Log.e("關閉", "wifi");

   }

記得加入下方權限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

本篇參考至:
http://developer.android.com/reference/android/location/Location.html
http://developer.android.com/reference/android/net/NetworkInfo.html