Android Cordova插件中的请求权限不会提示用户 [英] Request Permission in Android Cordova plugin does not prompt the user

查看:1936
本文介绍了Android Cordova插件中的请求权限不会提示用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Cordova插件,让Facebook聊天头像离线混合应用程序的浮动图标,需要SYSTEM_ALERT_WINDOW权限。

I am trying to write a Cordova Plugin to have a Facebook chat head like floating icon for ionic hybrid apps which requires SYSTEM_ALERT_WINDOW Permission.

由于Android M以后需要用户在应用程序第一次启动时授予权限,我正在尝试使用Cordova插件的 cordova.requestPermission(CordovaPlugin插件,int requestCode,String权限)方法来提示用户授予权限(如文档 )。

Since Android M onward requires the user to grant the permission at the first time the app starts, I am trying to use Cordova plugin's cordova.requestPermission(CordovaPlugin plugin, int requestCode, String permission) method to prompt the user to grant permission (as in the documentation).

public class Floatie extends CordovaPlugin {

    public static final String ACTION_START_FLOATIE = "startFloatie";
    public static final int REQUEST_CODE = 0;
    public static final String DRAW_OVER_OTHER_APPS = Manifest.permission.SYSTEM_ALERT_WINDOW;
    private CallbackContext callbackContext;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals(ACTION_START_FLOATIE)) {
            String message = args.getString(0); 
            this.callbackContext = callbackContext;

            if(cordova.hasPermission(DRAW_OVER_OTHER_APPS)) {
                Log.i("Floatie", "Has Permission");
            }
            else
            {
                getPermission(REQUEST_CODE);
            }

            return true;
        }
        return false;
    }

    protected void getPermission(int requestCode)
    {
        cordova.requestPermission(this, requestCode, DRAW_OVER_OTHER_APPS);
    }

    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
    {
        for(int r:grantResults)
        {
            if(r == PackageManager.PERMISSION_DENIED)
            {
                Log.i("Floatie", "Permission Denied");
                return;
            }
        }
        Log.i("Floatie", "Permission Granted");
    }
}

首次启动时,应用程序不会提示权限请求活动,而不是日志打印权限被拒绝。

At the first launch, the app does not prompt permission request activity and instead the log prints "Permission Denied".

我是Ionic和Cordova的新手,在花了几个小时之后仍然无法解决这个问题。任何帮助都将受到高度赞赏。

I am new to Ionic and Cordova and still couldn't work this out after spending hours on this. Any help will be highly appreciated.

提前致谢。

推荐答案

似乎 SYSTEM_ALERT_WINDOW 是一个特殊情况,它有自己的API 23开始的权限请求机制。在系统权限文档

Seems that SYSTEM_ALERT_WINDOW is a special case which has its own permission request mechanism starting with API 23. There's a mention of this in the System Permissions documentation:


有一些权限不像普通和危险的权限。 SYSTEM_ALERT_WINDOW和WRITE_SETTINGS特别敏感,因此大多数应用程序不应使用它们。如果应用程序需要其中一个权限,则必须在清单中声明权限,并发送请求用户授权的意图。系统通过向用户显示详细的管理屏幕来响应意图。
有关如何请求这些权限的详细信息,请参阅SYSTEM_ALERT_WINDOW和WRITE_SETTINGS参考条目。

There are a couple of permissions that don't behave like normal and dangerous permissions. SYSTEM_ALERT_WINDOW and WRITE_SETTINGS are particularly sensitive, so most apps should not use them. If an app needs one of these permissions, it must declare the permission in the manifest, and send an intent requesting the user's authorization. The system responds to the intent by showing a detailed management screen to the user. For details on how to request these permissions, see the SYSTEM_ALERT_WINDOW and WRITE_SETTINGS reference entries.

以及相应的 SYSTEM_ALERT_WINDOW 说:

And the corresponding reference entry for SYSTEM_ALERT_WINDOW says:


注意:如果应用程序的目标是API级别23或更高级别,则应用程序用户必须通过以下方式向应用程序明确授予此权限:权限管理屏幕。
该应用通过发送动作ACTION_MANAGE_OVERLAY_PERMISSION来请求用户的批准。该应用可以通过调用Settings.canDrawOverlays()来检查它是否具有此授权。

Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().

所以,简而言之,你不能使用 cordova.requestPermission()来请求 SYSTEM_ALERT_WINDOW ,你必须发送自定义意图:

So, in a nutshell, you can't use cordova.requestPermission() to request SYSTEM_ALERT_WINDOW, you've gotta send that custom intent:

cordova.getActivity().startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));

这篇关于Android Cordova插件中的请求权限不会提示用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆