Ionic Android 请求许可插件 - 不请求许可

Ionic Android ask permission plugin - not asking for permission

我正在调用下面的函数:

this.androidPermissions.requestPermission("ACCESS_FINE_LOCATION")
.then((data: any) {
if(data.hasPermission) {
    console.log("have permission");
   }
});

但是我没有得到权限弹出窗口。我已经尝试使用 catch 块,但没有看到任何错误。

更新 1: 我试过这个答案,但我仍然没有获得权限弹出窗口。

所以我认为这可能是由两件事造成的。

第一个是:您如何利用代码中的权限。

Android 26 and above: due to Android 26's changes to permissions handling (permissions are requested at time of use rather than at runtime,) if your app does not include any functions (eg. other Ionic Native plugins) that utilize a particular permission, then requestPermission() and requestPermissions() will resolve immediately with no prompt shown to the user. Thus, you must include a function utilizing the feature you would like to use before requesting permission for it.

据我了解,这不会在您的 catch 语句中被捕获,因为没有被捕获,本身没有错误。虽然它可能会出现在您的 then 语句中,但也有可能不是。尝试编写一个语句来查看 data: any 是否为空。有些插件在取消时会发送回调,有些则不会。这完全取决于开发人员的要求。

此外,我认为您请求的权限不正确。

改变这个

this.androidPermissions.requestPermission("ACCESS_FINE_LOCATION")
.then((data: any) {
if(data.hasPermission) {
    console.log("have permission");
   }
});

为此:

this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION).then((data:any) => {
   if(data.hasPermission) {
      console.log("have permission");
   }
});