简体   繁体   中英

Request runtime permissions on android

I am trying to get runtime permissions on my app. However the app is not displaying the dialog to request the specific permission. The code below only works when the GPS is turned on on the phone manually. I am running the app on Android 5.0. Thanks in advance.


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_location = findViewById(R.id.get_location);
        txt_latitude = findViewById(R.id.latitude);
        txt_longitude = findViewById(R.id.longitude);
        txt_timestamp = findViewById(R.id.timestamp);
        btn_map = findViewById(R.id.map);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        btn_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getLocation();
            }
        });

    }

    private void getLocation(){
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION_PERMISSION);
        } else {
            mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {

                @Override
                public void onSuccess(Location location) {
                    if(location != null){
                        mLastLocation = location;
                        txt_latitude.setText(getString(R.string.location_latitude,mLastLocation.getLatitude()));
                        txt_longitude.setText(getString(R.string.location_longitude,mLastLocation.getLongitude()));
                        txt_timestamp.setText(getString(R.string.timestamp,mLastLocation.getTime()));
                    } else {
                        txt_latitude.setText(R.string.no_latitude);
                        txt_longitude.setText(R.string.no_longitude);
                        txt_timestamp.setText(R.string.no_timestamp);

                    }
                }
            });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_LOCATION_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getLocation();
            } else {
                Toast.makeText(this, R.string.location_permission_denied, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Since you are run on Android 5.0 the Android runtime permission won't be shown. As runtime permission is only be shown starting from Android 6 (Marshmellow).

Please check the official documentation for the runtime permission details.

Happy Coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM