Java 为什么我的应用程序没有';当我第二次运行接受权限请求对话框时,是否显示位置按钮?
我正在用Java编写一个Android应用程序。该活动包含一个谷歌地图,我希望在顶部有一个按钮位置,如图所示: 我希望当我单击位置指向我的当前位置时,我使用类FusedLocationProviderClient 当我运行程序时,一切正常,直到我依次执行以下步骤: 1-第一次运行应用程序,我拒绝权限 2-第二次运行应用程序。它再次请求权限,所以我回答“确定”,但按钮没有出现。 3-我关闭了应用程序,因为我上次说了“是”,所以该按钮位于地图上,正在工作 这是我的密码:Java 为什么我的应用程序没有';当我第二次运行接受权限请求对话框时,是否显示位置按钮?,java,android,google-maps,android-permissions,fusedlocationproviderclient,Java,Android,Google Maps,Android Permissions,Fusedlocationproviderclient,我正在用Java编写一个Android应用程序。该活动包含一个谷歌地图,我希望在顶部有一个按钮位置,如图所示: 我希望当我单击位置指向我的当前位置时,我使用类FusedLocationProviderClient 当我运行程序时,一切正常,直到我依次执行以下步骤: 1-第一次运行应用程序,我拒绝权限 2-第二次运行应用程序。它再次请求权限,所以我回答“确定”,但按钮没有出现。 3-我关闭了应用程序,因为我上次说了“是”,所以该按钮位于地图上,正在工作 这是我的密码: private void
private void checkPermissions(int fineLocationPermission) {
if (fineLocationPermission != PackageManager.PERMISSION_GRANTED) { //If we don't have any permissions yet
// TODO: Consider calling
//We have to request permissions and in case the user refuse the permission we show an explanation of why he needs the permission
//The following method returns true in case the user reject the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
} else
mLocationPermissionGranted=true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If the user accepts the dialog box then we get the last location and show button
mLocationPermissionGranted=true;
} else {
mLocationPermissionGranted=false;
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
}
}
public void onLocationChanged(final Location location) {
String msg = "Updated location: " +
Double.toString(location.getLatitude()) + ", " +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//Show button to locate current position
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdateFactory.newLatLngZoom(latLng,12);
//mMap.setMaxZoomPreference(12);
return false;
}
});
// Add a marker our current position
LatLng CurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(CurrentPosition).title("Here you are!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(CurrentPosition));
}
@Override
public void onMapReady(GoogleMap googleMap) {
//Create the map
mMap = googleMap;
//Set the type of the map: HYBRID, NORMAL, SATELLITE or TERRAIN. In our case TERRAIN type
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//Set the zoom
mMap.setMaxZoomPreference(12);
//Set the markers
MarkerOptions markerOptions= new MarkerOptions();
//Checking permissions with the permissions we have included in the manifiest
checkPermissions(permissionCheckFineLocation);
if (mLocationPermissionGranted) {
try {
mMap.setMyLocationEnabled(true);
//Last location task
Task<Location> locationResult = mFusedLocationClient.getLastLocation();
locationResult.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
//Got last known location. In some rare situations this can be null
if (location != null) {
//Logic to handle location object
onLocationChanged(location);
}
}
});
} catch (SecurityException e) {
Log.e("error", e.getMessage());
}
}
}
private void checkPermissions(int fineLocationPermission){
如果(fineLocationPermission!=PackageManager.PERMISSION\u已授予){//如果我们还没有任何权限
考虑到呼叫
//我们必须请求权限,如果用户拒绝权限,我们会解释他为什么需要权限
//如果用户拒绝该权限,以下方法将返回true
ActivityCompat.requestPermissions(此,
新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},
权限(请求代码);
}否则
mLocationPermissionGratted=真;
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
开关(请求代码){
案例权限\请求\代码:
//如果取消请求,则结果数组为空。
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
//如果用户接受该对话框,那么我们将获得最后一个位置并显示按钮
mLocationPermissionGratted=真;
}否则{
mLocationPermissionGrassed=错误;
//权限被拒绝,嘘!禁用
//依赖于此权限的功能。
Toast.makeText(此“权限被拒绝”,Toast.LENGTH_LONG).show();
}
}
}
更改了公共无效位置(最终位置){
String msg=“更新的位置:”+
Double.toString(location.getLatitude())+“,”+
Double.toString(location.getLongitude());
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
最终LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
//显示按钮以定位当前位置
mMap.setOnMyLocationButtonClickListener(新的GoogleMap.OnMyLocationButtonClickListener(){
@凌驾
公共布尔onMyLocationButtonClick(){
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdateFactory.newLatLngZoom(latLng,12);
//mMap.setMaxZoomPreference(12);
返回false;
}
});
//在当前位置添加一个标记
LatLng CurrentPosition=新LatLng(location.getLatitude(),location.getLength());
mMap.addMarker(新的MarkerOptions().position(CurrentPosition).title(“给你!”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(当前位置));
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
//创建地图
mMap=谷歌地图;
//设置地图的类型:混合、普通、卫星或地形。在我们的例子中是地形类型
mMap.setMapType(GoogleMap.MAP\u TYPE\u地形);
//设置缩放
mMap.setMaxZoomPreference(12);
//设置标记
MarkerOptions MarkerOptions=新MarkerOptions();
//使用manifiest中包含的权限检查权限
检查权限(permissionCheckFineLocation);
如果(已授予位置许可){
试一试{
mMap.setMyLocationEnabled(真);
//最后位置任务
TaskLocationResult=mFusedLocationClient.getLastLocation();
locationResult.addOnSuccessListener(这是新的OnSuccessListener(){
@凌驾
成功时的公共无效(位置){
//已获取最后一个已知位置。在某些罕见情况下,此值可以为空
如果(位置!=null){
//处理位置对象的逻辑
onLocationChanged(位置);
}
}
});
}捕获(安全异常e){
Log.e(“error”,e.getMessage());
}
}
}
我知道这是个奇怪的问题,但我不知道如何表达自己。
任何帮助都会很好!
提前谢谢你
onMapReady
在授予权限后不会(再次)调用(这发生在onMapReady
之后)
将
if(mlocationpermissiongrated)
子句中onMapReady
中的代码移动到一个单独的方法,并从if
内部调用它,以及从onRequestPermissionsResult
中调用它(如果授予了权限)。onMapReady
在授予权限后不会(再次)调用它(这发生在onMapReady
之后)
如果(mLocationPermissionAllowed),则在
if(mLocationPermissionAllowed)中的onMapReady
中移动代码
子句调用一个单独的方法,并从if
以及onRequestPermissionsResult
if中调用它。谢谢,但最后我遵循了谷歌教程,其中有很好的解释。我想我有一个问题,就是不太理解我在做什么。谢谢你