简体   繁体   中英

If Permission Denied open a new activity android

I am requesting user to Access or deny gps permission if user clicks Access permission then my code is working but when the user clicks on Deny permission i am not able to open another activity

here is my code:

 txtLocation = (TextView)findViewById(R.id.txtLocation);

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

        boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Location location;

        if (network_enabled) {

            boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED;
            Log.d("permission", "onCreate: "+permissionGranted);
            if(permissionGranted) {
                // {Some Code}


            } else {

//               ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
         }


            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
                    200);

        }


            location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if(location!=null){
                lat = location.getLongitude();
                lon = location.getLatitude();

                Log.d("lat", "onCreate: "+lat+","+lon);
                txtLocation.setText(+lat+","+lon);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setMessage("Latitude:"+lat+"\n"+"Longitude:"+lon);
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();


            }
        }




    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {

        Log.d("", "onRequestPermissionsResult: "+requestCode);

        Toast.makeText(MainActivity.this, "code", Toast.LENGTH_SHORT).show();

        switch (requestCode) {
            case 200: {
                // If request is cancelled, the result arrays are empty.
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
                }else if(grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    Intent intent = new Intent(this,Homepage.class);
                    startActivity(intent);



                }
            }

            default:{

                Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

You are requesting two permissions with:

new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION}

But in onActivityResult() you are only looking at grantResults[0] .

Those are arrays: String permissions[], int[] grantResults .

Inspect them all.

Update:

ActivityCompat.requestPermissions(). 

You have that code twice. But the first one you commented. The second one is on the wrong place. Use only the first one.

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