【问题标题】:How can i know if user DENIED permission for GPS android我怎么知道用户是否拒绝了 GPS android 的权限
【发布时间】:2016-11-06 17:27:36
【问题描述】:

这是我的代码,如果我单击“允许”,它会询问我是否可以正常工作,但是当用户单击“拒绝”时,我想获得结果或记录被拒绝 如果用户单击“拒绝”权限,我该如何设置 这是我的代码:

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

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

    Location location;

    if (network_enabled) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            ;


        }

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


        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, ""+grantResults.length, Toast.LENGTH_SHORT).show();
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
                // permission was granted, yay! Do the

            } else {
                Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
                //user denied permission additionally you can check
                //ActivityCompat.shouldShowRequestPermissionRationale if user checked on "do not show again" while clicking deny
            }
            return;
        }

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

【问题讨论】:

    标签: android


    【解决方案1】:

    首先像这样实现一个 onRequestPermissionResult:

    @Override 
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_LOCATION: { 
                // If request is cancelled, the result arrays are empty. 
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the 
    
                } else { 
    
                   //user denied permission additionally you can check  
                   //ActivityCompat.shouldShowRequestPermissionRationale if user checked on "do not show again" while clicking deny
                } 
                return; 
            } 
    
            // other 'case' lines to check for other 
            // permissions this app might request 
        } 
    } 
    

    【讨论】:

    • 实现方法后如何调用onRequestPermissionsResult
    • 当用户按下“允许”或“拒绝”时会自动调用,
    • MY_PERMISSIONS_REQUEST_ACCESS_LOCATION 是什么?
    • 请通过此链接developer.android.com/training/permissions/requesting.html 了解如何处理marshamallow 上的运行时权限模型
    • 你能检查我的问题,因为我对代码进行了更改,但它不起作用
    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 2015-11-13
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2023-02-05
    相关资源
    最近更新 更多