简体   繁体   English

如果权限被拒绝,则打开一个新活动android

[英]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 如果用户单击“访问”权限,则我请求用户访问或拒绝gps权限,那么我的代码正在运行,但是当用户单击“拒绝”权限时,我无法打开其他活动

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] . 但是在onActivityResult()您仅查看grantResults[0]

Those are arrays: String permissions[], int[] grantResults . 这些是数组: 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. 仅使用第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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