private final static String TAG = "LocationUtils";
private static final boolean WCN_DISABLED = SystemProperties.get("ro.wcn").equals("disabled");
private static final boolean LOCATION_GPS_ENABLED = SystemProperties.get("persist.sys.location.gps").equals("enabled");
/**
* 手机是否开启位置服务
*/
public static boolean isLocServiceEnable(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
/**
* 手机是否开启位置服务
*/
public static boolean isLocationEnabled(Context context) {
ContentResolver resolver = context.getContentResolver();
int mode = Settings.Secure.getIntForUser(resolver, Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF, ActivityManager.getCurrentUser());
return mode != Settings.Secure.LOCATION_MODE_OFF;
}
/**
* 开启关闭Location
*/
public static boolean setLocationEnabled(Context context, boolean enabled) {
int currentUserId = ActivityManager.getCurrentUser();
final ContentResolver cr = context.getContentResolver();
int mode = enabled ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY : Settings.Secure.LOCATION_MODE_OFF;
if (enabled && (WCN_DISABLED || !LOCATION_GPS_ENABLED)) {
mode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
}
return Settings.Secure.putIntForUser(cr, Settings.Secure.LOCATION_MODE, mode, currentUserId);
}