我正在尝试请求ACCESS_FINE_LOCATION的权限并继续收到错误'无法解析符号' MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION',这是我的代码,有谁知道这是什么问题?
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
checkSelfPermission("Accept this to use location functionality");
}
else{
ActivityCompat.requestPermissions(GpMap.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
答案 0 :(得分:1)
您尚未在此Java类中的任何位置将MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
定义为int
值。
答案 1 :(得分:-1)
我不完全确定你是怎么做到的,但这是一个例子:
这是请求
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, PERMISSIONS_REQUEST_LOCATION);
}
return;
}
}
这是响应的处理程序:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case PERMISSIONS_REQUEST_LOCATION:{
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
setLastLocation();
}
return;
}
}
}