Android位置权限代码无法正常运行

生也有涯,知也无涯。这篇文章主要讲述Android位置权限代码无法正常运行相关的知识,希望能为你提供帮助。
我写了一个简单的代码,用于在运行时从用户获取位置权限。只是第一次它正常工作。之后,该权限对话框根本不显示。任何人都可以告诉我代码中缺少什么......

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); }} @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { mLocationPermissionGranted=false; switch (requestCode){ case LOCATION_PERMISSION_REQUEST_CODE:{ if(grantResults.length> 0){ for(int i=0; i< grantResults.length; i++){ if(grantResults[i]!=PackageManager.PERMISSION_GRANTED){ mLocationPermissionGranted=false; return; } } mLocationPermissionGranted=true; initMap(); } } } }

gradle依赖关系如下:
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.google.android.gms:play-services-maps:15.0.1' implementation 'com.google.android.gms:play-services-vision:15.0.2' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.google.android.gms:play-services-maps:15.0.1'

我是谷歌地图编码的新手,所以我对此知之甚少。任何机构都有解决方案请帮助我。提前致谢
答案【Android位置权限代码无法正常运行】你可以使用这段代码:
public static double longitude = 0.0d; public static double latitude = 0.0d; private FusedLocationProviderClient mFusedLocationClient; private long UPDATE_INTERVAL = 30 * 1000; /* 10 secs */ private long FASTEST_INTERVAL = 2000; /* 2 sec */

在oncreate中添加以下代码:
protected void startLocationUpdates() {LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setInterval(UPDATE_INTERVAL); mLocationRequest.setFastestInterval(FASTEST_INTERVAL); // Create LocationSettingsRequest object using location request LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); // Check whether location settings are satisfied // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient SettingsClient settingsClient = LocationServices.getSettingsClient(this); settingsClient.checkLocationSettings(locationSettingsRequest); final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){showGPSDisabledAlertToUser(); } return; else {// new Google API SDK v11 uses getFusedLocationProviderClient(this) LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { // do work here onLocationChanged(locationResult.getLastLocation()); } }, Looper.myLooper()); } }

另一答案
只是第一次它正常工作。之后,该权限对话框根本不显示
授予权限后,除非用户通过设置应用程序更改或重新安装应用程序等,否则无需再次允许

    推荐阅读