简体   繁体   English

调试时授予位置权限,但在android系统上未授予

[英]Permission for location is granted while debugging however not granted on android system

This is the debugging result for the check of the ACCESS_FINE_LOCATION permission, this means that the permission has been granted for the ACCESS_FINE_LOCATION 这是检查ACCESS_FINE_LOCATION权限的调试结果,这意味着已为ACCESS_FINE_LOCATION授予了权限

这是检查ACCESS_FINE_LOCATION权限的调试结果,这意味着已为ACCESS_FINE_LOCATION授予了权限

Now when I try to use the location service, i don't get any results, so i checked in the android system itself after debugging and found that the permission has not been granted to the location after it was already granted while debugging. 现在,当我尝试使用位置服务时,我没有得到任何结果,所以我在调试后检查了android系统本身,发现在调试之后已经授予了该位置的权限。

尚未授予该位置的许可

what could be the problem ? 可能是什么问题呢 ?

I had the same kind of issue in the past while trying to add runtime permissions to an existing app (unable to get the request dialog to appear...). 在尝试向现有应用程序添加运行时权限时,我遇到了同样的问题(无法让请求对话框出现......)。 Code looked good but was unable to get the dialog on a real device. 代码看起来不错但无法在真实设备上获得对话框。

My issue was that my target SDK was lower than Android 6.0 (API level 23). 我的问题是我的目标SDK低于Android 6.0(API级别23)。 You need to target at least API level 23 in order to use runtime permission (specified in the first part of this article ). 你需要至少API级别23的目标,以使用运行时的权限(在第一部分指定这篇文章 )。

Not sure if your issue is the same but I hope it could help :) 不确定你的问题是否相同,但我希望它可以帮助:)

use this library https://github.com/ParkSangGwon/TedPermission 使用此库https://github.com/ParkSangGwon/TedPermission

very useful!!! 很有用!!!

For the Android Version >= M , You must ask both Permission - ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION to get the location service. 对于Android Version >= M ,您必须同时询问Permission - ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION以获取位置服务。 A general Runtime Permission asking code: 一般Runtime Permission询问代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

    if (ActivityCompat.checkSelfPermission
            (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            &&
            ActivityCompat.checkSelfPermission
                    (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        }, 1); // 1 is requestCode
        return;

    }
    else{
         //permission already granted enable your location here
    }
}

Handle Request: 处理请求:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {

    case 1:
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
            Toast.makeText(your_activity.this,"PERMISSION_DENIED",Toast.LENGTH_SHORT).show();
          }
          else {
                Toast.makeText(your_activity.this,"PERMISSION_GRANTED",Toast.LENGTH_SHORT).show();
               // permission granted do something
          }
       break;
   }
}

我的问题解决但是我仍然不知道问题出在哪里,如果有人知道请告诉我,解决方案是我使用了一个名为https://github.com/hotchemi/PermissionsDispatcher的库,它请求了许可成功地,它还使用简化代码的注释。

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

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