【问题标题】:Location Permissions being requested again onRequestPermissionsResult再次请求位置权限 onRequestPermissionsResult
【发布时间】:2017-09-13 06:24:38
【问题描述】:

我正在尝试构建一个关于 Android N 中权限请求过程的简单示例。

我已经在我的清单文件中声明了ACCESS_COARSE_LOCATION 的用法。我正在尝试在自己的方法中处理请求:

public boolean permissionsChecker() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.READ_CONTACTS)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_REQUEST_PERMISSION_COARSE_LOCATION);
                return true;

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
        return false;

    }

应用程序连接时首先询问此问题:

@Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if(permissionsChecker()){
                mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            }
        }
        if(mLastLocation != null){
            Log.i(TAG, mLastLocation.getLatitude() + "");
            Log.i(TAG, mLastLocation.getLongitude() + "");
        } else {
            Log.i(TAG, "Still awaiting permissions, something went wrong");
        }

    }

我的问题是它仍然为 mLastLocation 实例返回 null 并且我能够在调试器中看到它,根据文档,一旦onRequestPermissionsResult() 中有请求代码,我就必须设置所有内容回调,但是当我尝试在其中设置 mLastLocation 实例时,我得到红线告诉我需要发出权限请求:

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case MY_REQUEST_PERMISSION_COARSE_LOCATION: {
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    // permissiosn was granted yaaaay! Do the coarse location related task you need to do
                    //Log.i(TAG, mLastLocation.getLongitude() + "");
                    //Log.i(TAG, mLastLocation.getLatitude() + "");
                    //mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                } else {
                    // permissions denied, boo! Disable the functionality that depends on this permissions
                }
                return;

                // Still inside the case checker, perform other permissions that this app might require working on
            } // end of case test for the acces coarse location
        } // end of stich statmeent
    } // end of onRequestPermissionsResult()

如果回调会让我再次请求权限,请求权限的目的是什么?我究竟做错了什么?我从文档中得到的东西不多(我知道缺乏经验),并且想知道如何解决这个问题。任何指针和代码建议将不胜感激!

【问题讨论】:

  • “我收到红线,告诉我我需要提出权限请求”——您会看到红色的下划线,表明 IDE 无法确定您是否仅在运行时执行此代码允许。 IDE 不是一个全能的全知神,这很好,因为一个有缺陷的神可能非常有问题。 “想知道如何解决这个问题”——暂时忽略它。运行你的代码。一旦您的代码在此区域中运行,请使用快速修复添加 @SuppressLint 注释以消除红色下划线。
  • 你在哪里打电话mGoogleApiClient.connect();..发布完整代码..
  • @rafsanahmad007 正在 onStart() 方法中完成一项

标签: java android android-permissions android-location


【解决方案1】:

permissionChecker() 的用法看起来像是在扔东西。仅仅因为您请求了权限并不意味着它已被授予。

本次 Droicon 演讲将帮助解释权限模型和用法:https://youtu.be/WGz-alwVh8A

您可能还会发现此库有助于使您的代码更简单、更易于遵循:https://github.com/hiqes/andele

【讨论】:

  • 用户点击“允许”按钮不就意味着权限被授予了吗?
  • 是的,但是在调用 onRequestPermissionResult() 通知您它已被授予之前,您的应用不会知道发生了这种情况。
  • 如果我错了,请纠正我。至于我从文档中了解的内容,当您需要请求特定权限时(例如 permissionsChecker()),权限检查器然后显示对话框,用户接受它,然后将结果传递给回调,然后如果用户给予了许可,它可以让你做任何你想做的事情。我的观点是,那时它仍然不会让我实例化该位置,我不知道为什么。感谢您提供指向 github 存储库的链接!它看起来很有用,我会尽快研究它。
  • 是的,尽管您的代码的结构方式假定当调用requestPermissions() 时立即停止执行,然后在获得用户响应后继续执行。不是这种情况。 requestPermissions() 是一个异步调用,就像发送一个Intent。因此,您的代码将在当前连接 (onConnected) 中完成执行,然后稍后在 onRequestPermissionsResult() 处被回调,您可以在此处确定是否允许您进行受保护的调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 2018-12-21
相关资源
最近更新 更多