我在我的项目中使用谷歌地图代码..我还在清单文件中添加了权限但我的代码中也有错误说添加权限..所以我从网上搜索并发现动态添加权限..所以我添加了以下代码..
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}
所以现在错误是无法解析符号上下文..我正在使用sdk 23 ..请帮助我
答案 0 :(得分:1)
private void checkPermissionForGettingLocation() {
final int fineLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
final int coarseLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
final int callPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (fineLocationPermission == PackageManager.PERMISSION_GRANTED && coarseLocationPermission == PackageManager.PERMISSION_GRANTED && callPhonePermission == PackageManager.PERMISSION_GRANTED) {
// getLocationAndSaveInDatabaseOrEnableGPS();
showGpsDialogAndGetLocation();
} else {
boolean requestPermissionRationale = ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
if (requestPermissionRationale) {
Snackbar snackbar = Snackbar.make(mRootCL, "Please provide location permission for app to work correctly. ", Snackbar.LENGTH_INDEFINITE)
.setAction("Settings", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", LoginActivity.this.getPackageName(), null);
intent.setData(uri);
LoginActivity.this.startActivity(intent);
}
});
snackbar.setActionTextColor(LoginActivity.this.getResources().getColor(R.color.button_green_color));
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(LoginActivity.this.getResources().getColor(R.color.white));
snackbar.show();
//Toast.makeText(LoginActivity.this, "Location permission are required to get location.", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.CALL_PHONE}, LOCATION_PERMISSION_REQUEST_CODE);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// getLocationAndSaveInDatabaseOrEnableGPS();
showGpsDialogAndGetLocation();
}
break;
}
}
答案 1 :(得分:1)
请求如下许可:
try {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasReadContactPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
if (hasReadContactPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
101);
}
}
} catch (Exception e) {
e.printStackTrace();
}
还在下面添加声明:
final private int REQUEST_CODE_ASK_PERMISSIONS = 124;
感谢。!!