requestLocationUpdates权限
我有一个Android应用程序发送GPS位置到DB。 我今天测试过,发现位置管理器需要调用requestLocationUpdates
的权限。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上的某些默认权限可能已更改吗?如果是的话,我该如何改变它?
你将不得不写一种方法来检查用户是否授予权限。如果没有,则请求用户许可。 Constants.LOCATION_PERMISSION_REQUEST只是requestcode匹配时onRequestpermission结果
public static boolean checkLocationPermission(Activity activity){
if(ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_PERMISSION_REQUEST);
return false;
}
return true;
}
了回调当用户允许,你会得到回调上覆盖的方法“onRequestPermissionsResult()”
我认为这对我很有用。你能告诉我什么是“WaselGiftConstants”吗? –
@ ch.ihssen它只是请求代码 –
你能解释请求代码更多我无法解决它在我的应用程序。你有什么想法可以帮助。谢谢 –
你必须从运行时间的棉花糖版本的机器人请求权限。通过这个link
进去的Android 6.0(API级别23)开始,用户授予权限的应用程序应用程序运行时,而不是当他们安装应用程序。此方法简化了应用程序安装过程,因为用户在安装或更新应用程序时无需授予权限。它还使用户可以更好地控制应用程序的功能;例如,用户可以选择给相机应用程序访问相机,但不能访问设备位置。用户可以随时撤销权限,只需转到应用的“设置”屏幕即可。
我们用下面的代码来检查的位置的权限:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
是的我实际上在询问这段代码。我的设备是API 17. –
你怎么能解释它几天前在我的设备上正常工作的事实。 –
可能是您更改了'buildToolsVersion'或'targetSdkVersion'或'compileSdkVersion' –
这是检查权限 com.github.fccaikai最好的图书馆:AndroidPermissionX:1.0.0
例代码:
final PermissionCompat.Builder builder = new PermissionCompat.Builder(activity);
builder.addPermissions(new String[]{
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
});
builder.addPermissionRationale(activity.getString(R.string.permission_rationale));
builder.addRequestPermissionsCallBack(new OnRequestPermissionsCallBack() {
@Override
public void onGrant() {
Log.d(TAG, "Grant");
}
@Override
public void onDenied(String permission) {
Log.e(TAG, "Denied");
}
});
builder.build().request();
我想获取GPS位置不是摄像机 –
这只是一个例子。将它们更改为您的权限。 –
*我想设置权限允许的我不想问设备或用户的许可。*太糟糕了,你想要的是不可能 –
你可以发布一些代码吗? –
@Sandeepdhiman我添加了它。 –