Android checkSelfPermission,requestPermissions多次调用

时间:2016-08-29 11:34:29

标签: android permissions location

我使用ActivityCompat.checkSelfPermission()来检查用户是否已经允许位置访问,然后ActivityCompat.requestPermissions(),如果他已经没有。问题是我不明白它为什么会这样做requestPermissions()ActivityCompat.checkSelfPermission()在第一次之后应该是真的,它不应该再向用户请求权限3次...谢谢为了你的帮助。## Heading ##

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

    private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private static final int LOCATION_UPDATE_MIN_TIME = 400;
    private static final int LOCATION_UPDATE_MIN_DISTANCE = 1;
    private static final double INIT_LAT = 48.8583701;
    private static final double INIT_LON = 2.2922926;
    private static final float CAMERA_ZOOM = 15f;
    private boolean pg = false;

    private enum LocationUpdateFrequency {
        SINGLE, PERIODIC
    }

    private enum LocationUpdateAction {
        REMOVE, REQUEST, LAST_KNOWN
    }

    private GoogleMap mMap;
    private LocationManager mLocationManager;
    private String provider;
    private Location mLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        Log.i("[INFO]","0");

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if(provider == null) {
            Log.i("[INFO]","1");
            setProvider();
        }

        updateLocation(LocationUpdateAction.LAST_KNOWN);

        if(mLocation == null) {
            Log.i("[INFO]","2");
            updateLocation(LocationUpdateFrequency.PERIODIC,LocationUpdateAction.REQUEST);
        }
    }

    /*
        Called for cancel location updates and get last know location : no updatefrequency needed
     */
    private void updateLocation(LocationUpdateAction updateAction) {
        if(updateAction == LocationUpdateAction.LAST_KNOWN || updateAction == LocationUpdateAction.REMOVE) {
            updateLocation(null,updateAction);
        } else {
            throw  new IllegalArgumentException("Incorrect parameter");
        }
    }

    private void updateLocation(LocationUpdateFrequency updateFrequency, LocationUpdateAction updateAction) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Log.i("[INFO]", "3");

                //if(!pg) {
                    ActivityCompat.requestPermissions(MapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
                //pg = true;
            //}
                return;
            }
        }
        Log.i("[INFO]","3-a");
        switch (updateAction) {
            case REQUEST : {
                switch (updateFrequency) {
                    case PERIODIC : {
                        Log.i("[INFO]","4");
                        mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, this);
                        return;
                    }
                    case SINGLE : {
                        Log.i("[INFO]","5");
                        mLocationManager.requestSingleUpdate(provider,this,null);
                        return;
                    }
                }
                return;
            }
            case REMOVE : {
                Log.i("[INFO]","6");
                mLocationManager.removeUpdates(this);
                return;
            }
            case LAST_KNOWN : {
                Log.i("[INFO]","7");
                mLocationManager.getLastKnownLocation(provider);
            }
        }
    }

    private void setProvider() {
        Log.i("[INFO]","8");
        provider = mLocationManager.getBestProvider(new Criteria(),false);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.i("[INFO]", "9");
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MapsActivity.this,
                            "permission was granted, :)",
                            Toast.LENGTH_LONG).show();
                    if(provider == null) {
                        setProvider();
                    }
                } else {
                    Toast.makeText(MapsActivity.this,
                            "permission denied, ...:(",
                            Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Log.i("INFO","10");
        // Add a marker in Sydney and move the camera
        LatLng initialPosition = new LatLng(INIT_LAT, INIT_LON);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(initialPosition));
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i("INFO","11");
        if (location != null) {
            CameraPosition cameraPosition = CameraPosition.builder()
                    .target(new LatLng(location.getLatitude(),location.getLongitude()))
                    .zoom(CAMERA_ZOOM)
                    .build();

            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.i("[INFO]","12");
    }

    @Override
    public void onProviderEnabled(String s) {
        Log.i("[INFO]","13");
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.i("[INFO]","14");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("[INFO]","15");
        updateLocation(LocationUpdateAction.REMOVE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("[INFO]","16");
        updateLocation(LocationUpdateFrequency.PERIODIC,LocationUpdateAction.REQUEST);
    }
}

0 个答案:

没有答案