【发布时间】:2017-08-24 12:39:35
【问题描述】:
我有一个将 GPS 位置发送到 DB 的 Android 应用程序。
今天测试了一下,发现location manager需要权限才能拨打requestLocationUpdates。
我想将权限设置为允许,我不想向设备或用户请求权限。
现在,代码需要添加一些代码来请求权限?
/**
* Enable location services
*/
public void connect() {
Log.d(TAG, ".connect() entered");
// Check if location provider is enabled
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (!locationManager.isProviderEnabled(locationProvider)) {
Log.d(TAG, "Location provider not enabled.");
app.setCurrentLocation(null);
return;
}
// register for location updates, if location provider and permission are available
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
locationManager.requestLocationUpdates(bestProvider, Constants.LOCATION_MIN_TIME, Constants.LOCATION_MIN_DISTANCE, this);
app.setCurrentLocation(locationManager.getLastKnownLocation(locationProvider));
}
}
/**
* Disable location services
*/
public void disconnect() {
Log.d(TAG, ".disconnect() entered");
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (locationManager.isProviderEnabled(locationProvider)) {
locationManager.removeUpdates(this);
}
}
我也添加了清单。 PS:此权限是新要求的,因为我几天前一直在同一设备(Android 4.4)上测试该应用程序,并且运行良好。 自 2017/08/21 以来,Android 上的某些默认权限是否可能已更改?如果可以,我该如何更改?
【问题讨论】:
-
我想将权限设置为允许,我不想向设备或用户请求权限。太糟糕了,你想要的是不可能的
-
你能贴一些代码吗?
-
@Sandeepdhiman 我已经添加了。
标签: android permissions location locationmanager