Android Cordova 插件中的 Request Permission 不提示用户

Request Permission in Android Cordova plugin does not prompt the user

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

由于Android M onward需要用户在应用程序第一次启动时授予权限,我尝试使用Cordova插件的cordova.requestPermission(CordovaPlugin plugin, int requestCode, String permission)方法来提示用户授予权限(如在 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");
    }
}

首次启动时,应用程序不会提示权限请求 activity,而是日志打印 "Permission Denied"。

我是 Ionic 和 Cordova 的新手,花了几个小时后仍然无法解决这个问题。任何帮助将不胜感激。

提前致谢。

似乎 SYSTEM_ALERT_WINDOW 是一个特例,它有自己的以 API 开头的权限请求机制 23. System Permissions documentation:

中提到了这一点

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.

对应的reference entry for SYSTEM_ALERT_WINDOW表示:

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,您必须发送该自定义意图:

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