我编写了一个简单的代码,用于在运行时从用户那里获取位置权限。只有第一次它正常工作。之后,权限对话框根本不会出现。任何人都可以告诉我代码中缺少什么...
public static final String FINE_LOCATION=Manifest.permission.ACCESS_FINE_LOCATION;
public static final String COARSE_LOCATION=Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE=1234;
private boolean mLocationPermissionGranted=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
getLocationPermission();
}
public void initMap() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
}
public void getLocationPermission(){
String [] permissions={Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
mLocationPermissionGranted=true;
initMap();
} else {
ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
}
}
else {
ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
}
}
我是谷歌地图编码的新手,所以我对此知之甚少。任何机构有解决方案请帮助我。提前致谢
茅侃侃
相关分类