【问题标题】:Android Location permissions code does not work correctlyAndroid 位置权限代码无法正常工作
【发布时间】:2018-09-07 17:51:57
【问题描述】:

我编写了一个简单的代码,用于在运行时从用户那里获取位置权限。 仅在第一次正常工作。之后,权限对话框根本不出现。任何人都可以告诉我代码中缺少什么...

  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'

我是谷歌地图编码的新手,所以我对此知之甚少。任何机构都有解决方案请帮助我。提前致谢

【问题讨论】:

    标签: java android google-maps location geocoding


    【解决方案1】:

    仅第一次正常工作。获得许可后 对话框根本不出现

    一旦授予权限,则无需再次允许,除非用户通过设置应用程序更改它或重新安装应用程序等

    【讨论】:

      【解决方案2】:

      你可以使用这个代码:

      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());
      
          }
      }
      

      【讨论】:

      • public void onLocationChanged(Location location) { // 新位置现已确定 String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double. toString(location.getLongitude());纬度 =location.getLatitude() ;经度 = location.getLongitude() ; }
      • private void showGPSDisabledAlertToUser() { Intent callGPSSettingIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);开始活动(调用GPSSettingIntent); }
      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2019-11-24
      相关资源
      最近更新 更多