Phonegap WebIntent无法打开/访问下载的APK文件

时间:2017-07-21 00:08:43

标签: android cordova phonegap-plugins cordova-plugins phonegap

从我的Phonegap应用程序我正在下载我的APK,没有任何问题,但我无法在其中安装文件。

以下位代码在WEB INTENT部分失败,该部分应安装APk但无法读取文件。

var apkFilePath = cordova.file.externalApplicationStorageDirectory+'myapp.apk';
// Android UPDATE routine
function downloadApkAndroid(data) {
  var permissions = cordova.plugins.permissions;
   permissions.hasPermission(permissions.WRITE_EXTERNAL_STORAGE, function (status) {
     if (!status.hasPermission) {
       var errorCallback = function () {
         alert("Error: app requires storage permission");
         if (callBack && callBack !== null) {
           callBack();
         }
       };
       permissions.requestPermission(permissions.WRITE_EXTERNAL_STORAGE,
         function (status) {
           if (!status.hasPermission)
             errorCallback();
           else {
             downloadFile();
           }
         },
         errorCallback);
     }
     else {
       downloadFile();
     }
   }, null);
}

function downloadFile(){
  var fileTransfer = new FileTransfer();
  var url = "https://myappurl.com/myapp.apk";
  var uri = encodeURI(url);
  var filePath = getFilePath();

  fileTransfer.download(
    uri,
    apkFilePath,
    function (entry) {
      console.log("Download complete: " + entry.fullPath);
      promptForUpdateAndroid(entry);
    },
    function (error) {
      console.error("Download error source " + error.source);
      console.error("Download error target " + error.target);
      console.error("Download error code " + error.code);
    },
    false,
    {

    }
  );
}


/*
 * Uses the borismus webintent plugin
 */
 function promptForUpdateAndroid(entry) {
  console.log(apkFilePath);
  window.plugins.webintent.startActivity(
    {
      action: window.plugins.webintent.ACTION_VIEW,
      url: apkFilePath,
    type: 'application/vnd.android.package-archive'
  },
  function () {
  },
  function () {
    // alert('Failed to open URL via Android Intent.');
    console.log("Failed to open URL via Android Intent. URL: " + entry.fullPath);
  }
  );
}

1 个答案:

答案 0 :(得分:0)

通过使用cordova-plugin-file-opener2并更改promptForUpdateAndroid()以摆脱Web意图来解决这个问题:

var myFilePath = cordova.file.dataDirectory+'myApp.apk';

function promptForUpdateAndroid(entry) {
  cordova.plugins.fileOpener2.open(
    myFilePath,
    'application/vnd.android.package-archive',
    {
      error : function(e) {
      console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
    },
    success : function () {
      console.log('file opened successfully');
    }
  }
);

这会立即安装应用程序,但安装后它不会重新打开。我想知道这件事。