【问题标题】:Can any OnClickListener be added on Google's location permission dialog box?Google 的位置权限对话框可以添加任何 OnClickListener 吗?
【发布时间】:2019-09-05 10:50:36
【问题描述】:

我正在 Android Studio 中制作一个基于位置的应用程序,当用户第一次启动应用程序时,它会要求用户授予访问设备位置的权限。现在我想在用户单击 Google 对话框中的 DENY 按钮时添加一条错误消息,要求用户授予访问该位置的权限,因为如果用户拒绝授予访问该位置的权限,我的应用程序将无法运行。我该怎么做?

【问题讨论】:

标签: android android-studio location


【解决方案1】:

尝试使用此代码:-

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_PERMISSION) {
        for (int i = 0, len = permissions.length; i < len; i++) {
            String permission = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
            // user rejected the permission
                boolean showRationale = shouldShowRequestPermissionRationale( permission );
                if (! showRationale) {
                    // user also CHECKED "never ask again"   
                } 
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    是的,只需按照以下方式进行

     @Override protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         //.........
    
         checkLocationPermission(); }
    

    然后在onCreate()中调用checkLocationPermission()方法:

      public static final int REQUEST_LOCATION = 99;
    
        public boolean checkLocationPermission() {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
    
    
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)) {
    
    
                    new AlertDialog.Builder(this)
                            .setTitle(R.string.title_location_permission)
                            .setMessage(R.string.text_location_permission)
                            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ActivityCompat.requestPermissions(MainActivity.this,
                                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                            |REQUEST_LOCATION);
                                }
                            })
                            .create()
                            .show();
    
    
                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            REQUEST_LOCATION);
                }
                return false;
            } else {
                return true;
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case REQUEST_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
                        // location-related task you need to do.
                        if (ContextCompat.checkSelfPermission(this,
                                Manifest.permission.ACCESS_FINE_LOCATION)
                                == PackageManager.PERMISSION_GRANTED) {
    
                            //Request location updates:
                            locationManager.requestLocationUpdates(provider, 400, 1, this);
                        }
    
                    } else {
    
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
    
                    }
                    return;
                }
    
            }
        }
    

    【讨论】:

    • 它有用吗?
    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 2012-05-10
    • 2017-05-06
    • 2020-07-09
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多