我正在嘗試編寫一個Cordova插件,讓Facebook聊天頭像浮動圖標,用於需要SYSTEM_ALERT_WINDOW權限的離子混合應用程序。在Android Cordova插件的請求權限不會提示用戶
由於Android M向前版本要求用戶在應用程序第一次啓動時授予權限,因此我試圖使用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");
}
}
在第一次啓動時,應用程序不會提示權限請求活動,而是日誌打印「權限被拒絕」。
我是Ionic和Cordova的新手,在花費數小時後仍然無法工作。任何幫助將不勝感激。
在此先感謝。
我知道這是不相關的問題,但你打算在任何一個機會讓你的項目開源? –