Android,androidadt
位置定位(Location)服務(Service)類的基本操作
本文地址: http://blog.csdn.net/caroline_wendy
定位服務(Location Service),可以確定行動裝置的地址,在地圖相關服務中,經常會使用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;/** * Created by wangchenlong on 14-11-17. * * 定位服務的庫: * 包含功能:判斷是否啟動 定位服務、網路連接、WIFI串連 * 頁面跳轉-> 定位服務設定介面,WIFI設定介面 */public class LocationServiceUtils { private static final String TAG = "LocationServiceUtils"; /** * 判斷是否啟動定位服務 * * @param context 全域資訊介面 * @return 是否啟動定位服務 */ 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; } /** * 判斷是否啟動全部網路連接,包括WIFI和流量 * * @param context 全域資訊介面 * @return 是否串連到網路 */ 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; } /** * 判斷是否啟動WIFI串連 * * @param context 全域資訊介面 * @return 是否串連到WIFI */ 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; } /** * 跳轉定位服務介面 * * @param context 全域資訊介面 */ 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); } /** * 跳轉WIFI服務介面 * * @param context 全域資訊介面 */ 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); }}
工具類的靜態方法可以直接使用。