位置定位(Location)服務(Service)類的基本操作
本文位址: http://blog.csdn.net/caroline_wendy
定位服務,可以确定移動裝置的位址,在地圖相關服務中,經常會使用GPS和移動相關的定位服務,GPS較為精準。
根據常用的定位服務功能,又添加網絡檢測和Wifi檢測,和啟動相關界面進行測試的功能。
代碼:
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
public class LocationServiceUtils {
private static final String TAG = "LocationServiceUtils";
public static boolean isOpenLocService(final Context context) {
boolean isGps = false; //判斷GPS定位是否啟動
boolean isNetwork = false; //判斷網絡定位是否啟動
if (context != null) {
LocationManager locationManager
= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
//通過GPS衛星定位,定位級别可以精确到街(通過24顆衛星定位,在室外和空曠的地方定位準确、速度快)
isGps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//通過WLAN或移動網絡(3G/2G)确定的位置(也稱作AGPS,輔助GPS定位。主要用于在室内或遮蓋物(建築群或茂密的深林等)密集的地方定位)
isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
if (isGps || isNetwork) {
return true;
}
}
return false;
}
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
public static boolean isWifiConnected(Context context) {
if (context != null) {
WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
if (wifi != null) {
return wifi.isWifiEnabled();
}
}
return false;
}
public static void gotoLocServiceSettings(Context context) {
final Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public static void gotoWifiServiceSettings(Context context) {
final Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
工具類可以直接使用。
原文:http://blog.csdn.net/caroline_wendy/article/details/41226391