android 获取手机GPS当前位置的经纬度
Android平台在开发过程中主要使用LocationManager和LocationProviders对象。
- LocationManager:
用来管理Location功能相关
- LocationProviders:
提供四种不能的定位功集合
public static final String GPS_PROVIDER = "gps"; //GPS方式定位
public static final String NETWORK_PROVIDER = "network"; //网络定位
public static final String PASSIVE_PROVIDER = "passive"; //被动定位
public static final int LOCATION_MODE_SENSORS_ONLY = 1;
public static final int LOCATION_MODE_BATTERY_SAVING = 2;
public static final int LOCATION_MODE_HIGH_ACCURACY = 3;
低版本的android源码中
case LOCATION_MODE_OFF:
break;
case LOCATION_MODE_SENSORS_ONLY:
gps = true;
break;
case LOCATION_MODE_BATTERY_SAVING:
network = true;
break;
case LOCATION_MODE_HIGH_ACCURACY:
gps = true;
network = true;
break;
default:
使用GPS、WLAN和移动网络 | 使用WLAN和移动网络 | 仅使用GPS | |
---|---|---|---|
截图 | |||
特点 | 同时使用GPS、WIFI及基站定位,速度快、精度高,室内定位效果好。 缺点:耗流量、耗电量 | 只使用WIFI和基站定位,需要WIFI或者基站才行,室内效果好。 缺点:依赖WIFI或基站,精度一般 | 不依赖WIFI和基站,室内效果差,户外可靠性好。 缺点:室内效果差 |
代码 | LocationManager.GPS_PROVIDER = true; LocationManager.NETWORK_PROVIDER = true; | LocationManager.GPS_PROVIDER = false; LocationManager.NETWORK_PROVIDER = true; | LocationManager.GPS_PROVIDER = true; LocationManager.NETWORK_PROVIDER = false; |
是否打开定位服务 | Settings.Secure.LOCATION_MODE = 3 | Settings.Secure.LOCATION_MODE = 2 | Settings.Secure.LOCATION_MODE = 1 |
aosp/frameworks/base/core/java/android/provider/Settings.java
@Deprecated
public static boolean isLocationProviderEnabled(ContentResolver cr, String provider) {
String allowedProviders = Settings.Secure.getStringForUser(cr,
LOCATION_PROVIDERS_ALLOWED, cr.getUserId());
return TextUtils.delimitedStringContains(allowedProviders, ',', provider);
}
/**
* Thread-safe method for enabling or disabling a single location provider. This will have
* no effect on Android Q and above.
* @param cr the content resolver to use
* @param provider the location provider to enable or disable
* @param enabled true if the provider should be enabled
* @deprecated This API is deprecated
*/
@Deprecated
public static void setLocationProviderEnabled(ContentResolver cr,
String provider, boolean enabled) {
}
@TestApi
public static final String FUSED_PROVIDER = "fused"; //测试API接口,是一个系统级隐藏API,开发过程中可直接忽略,
1 //判断指定方式的定位服务是否开启
1 Log.i("androidodm", "GPS是否打开 " + mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
2 Log.i("androidodm", "网络定位是否打开 " + mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
2 //判断系统是否开启定位服务(不限指定位置服务提供者)
boolean enable = mLocationManager.isLocationEnabled();
//代码开启GPS方式位置服务
Log.d(TAG, "Enable GPS time sync");
LocationManager mLocationManager = (LocationManager) mContext.getSystemService( Context.LOCATION_SERVICE);
boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
Log.d(TAG, "Enable GPS time sync gpsEnabled =" + gpsEnabled);
int currentUserId = ActivityManager.getCurrentUser();
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY,currentUserId);
}
///aosp/frameworks/base/services/core/java/com/android/server/location/SettingsHelper.java 开启定位服务的API接口实现,
/**
* Location mode is off.
*/
public static final int LOCATION_MODE_OFF = 0;
public void setLocationEnabled(boolean enabled, int userId) {
long identity = Binder.clearCallingIdentity();
try {
Settings.Secure.putIntForUser(
mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE,
enabled
? Settings.Secure.LOCATION_MODE_ON
: Settings.Secure.LOCATION_MODE_OFF,
userId);
} finally {
Binder.restoreCallingIdentity(identity);
}
}
//Q及Q以上版本
boolean nlpSuccess = Settings.Secure.setLocationProviderEnabled( cr, LocationManager.NETWORK_PROVIDER, true/false);
boolean gpsSuccess = Settings.Secure.setLocationProviderEnabled( cr, LocationManager.GPS_PROVIDER, true/false);
//end
public static boolean isLocationEnable(Context context) {
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_OFF);
return mode != Settings.Secure.LOCATION_MODE_OFF;
}
public static void setLocationEnable(Context context, boolean enable) {
if (enable) {
Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
} else {
Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
}
}
public static boolean isLocationGpsOnly(Context context) {
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
return mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
}
public static void setLocationGpsOnly(Context context) {
Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
}
private double latitude=0.0;
private double longitude =0.0;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){//判断设备的GPS定位功能是否开启
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Location location = locationManager.getLastKnownLocation(provider);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}else{
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
//doString
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
//doString
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
//doString
}
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
}
}
};
/**服务管理对象的监听器*/
//参数1:定位的方式 参数2:监听更新间隔时间(ms) 参数3:监听更新的距离(m) 参数4:监听的方法
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude(); //经度
longitude = location.getLongitude(); //纬度
}
}//else
另一个判断设备GPS定位是否开启的判断方式:
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER );
当有一些特殊APP(如SOS功能APP)需要在不需人为介入的情况下自动开启GPS定位功能,则使用如下代码,需要android.permission.WRITE_SECURE_SETTINGS权限
Settings.Secure.setLocationProviderEnabled( context.getContentResolver(), LocationManager.GPS_PROVIDER, true );
自动跳转到Settings的GPS功能界面"设置-位置信息访问权限"进行设置.
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(settingsIntent);
同时别忘了在AndroidManifest.xml文件中加入如下权限:
<!-- 连接互联网Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//封装GPS功能成工具类 https://blog.csdn.net/u012810020/article/details/52517976/
评论