目录
GPS定位开发步骤
1. 权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 允许一个程序访问精良位置(如GPS)-->
Android10 需要添加后台定位权限
<!-- Required only when requesting background location access on
Android 10 (API level 29). -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
2.代码流程
2.1. 获取LocationManager
LocationManager locationManager;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
2.2. 判断是否支持GPS
/**
* 判断是否支持GPS
*
* @param context
* @return
*/
public boolean hasGPSDevice(Context context) {
final LocationManager mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (mgr == null)
return false;
final List<String> providers = mgr.getAllProviders();
if (providers == null)
return false;
return providers.contains(LocationManager.GPS_PROVIDER);
}
@Override
protected void onResume() {
super.onResume();
boolean flag = hasGPSDevice(context);
if (flag) {
requestPermissions();
}
}
2.3. 获取定位权限
private void requestPermissions() {
AndPermission.with(this)