天天看点

Android定位服务service,Android - 位置定位(Location)服务(Service)类的基本操作

位置定位(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