【问题标题】:Android M and above location course and location fine returning permission granted trueAndroid M及以上位置课程和位置精细返回权限授予true
【发布时间】:2017-02-27 06:27:00
【问题描述】:

我正在尝试访问用户的位置,只是整理出事情的权限方面。所以我的理解是,在 23 及以上,无论清单如何,用户都必须授予权限。

所以对于旧版本,我在清单中有这样的内容:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

对于较新的 >= 23,我在显示对话框之前进行这样的测试:

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}

我理解的方式应该是第一次返回 false,但它返回的是 true。

用户是否真的必须禁用定位服务,或者它被认为是“危险的”并且必须在第一时间获得批准?

另外我正在使用一个新的模拟器 api 23,当我查看位置权限时,它说没有应用程序请求位置。

感谢您的帮助

【问题讨论】:

  • 您是否在片段中检查这种情况?
  • 是的,我是这样会改变它吗?
  • 在片段的on start方法中
  • @MadhukarHebbar 谢谢,该应用的权限设置为开启。我以为这不是默认的?
  • @MadhukarHebbar 这是导致问题的原因 谢谢!由于某种原因,模拟器已经开始将该权限设置为true。你知道那会发生什么吗?

标签: android android-6.0-marshmallow android-permissions


【解决方案1】:

试试这个。

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}

【讨论】:

  • 感谢您的回复。抱歉,这并没有改变任何东西。
【解决方案2】:

试试这个:

private boolean hasRunTimeLocationPermission() {
 if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
        {
            return false;
        }
 else
        {
           return true;
        }
      }

【讨论】:

    【解决方案3】:

    像这样检查权限:-

    Check permissions like this :-
    
    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
                //Show Information about why you need the permission
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Need Location Permission");
                builder.setMessage("This app needs location permission.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            } else if (permissionStatus.getBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,false)) {
                //Previously Permission Request was cancelled with 'Dont Ask Again',
                // Redirect to Settings after showing Information about why you need the permission
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Need Location Permission");
                builder.setMessage("This app needs location permission.");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        sentToSettings = true;
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);
                        intent.setData(uri);
                        startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                        Toast.makeText(getBaseContext(), "Go to Permissions to Grant Location", Toast.LENGTH_LONG).show();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            } else {
                //just request the permission
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
            }
    
            SharedPreferences.Editor editor = permissionStatus.edit();
            editor.putBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,true);
            editor.commit();
    
    
        } else {
            //You already have the permission, just go ahead.
            proceedAfterPermission();
        }
    

    【讨论】:

      【解决方案4】:

      即使对于 api>23 也将其添加到 Manifest

      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      

      然后像这样请求运行时权限:-

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

      要处理结果,请使用:-

      @Override
      public void onRequestPermissionsResult(int requestCode,
                                         String permissions[], int[] grantResults) {
      switch (requestCode) {
          case 1: {
      
            // 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 {
      
                  // permission denied, boo! 
                  Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
              }
              return;
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-28
        • 2021-02-14
        • 2017-05-30
        • 2019-05-21
        • 2021-01-22
        • 2015-12-16
        • 2016-12-08
        • 1970-01-01
        相关资源
        最近更新 更多