【问题标题】:Phonegap WebIntent cannot open / access a downloaded APK filePhonegap WebIntent 无法打开/访问下载的 APK 文件
【发布时间】:2017-07-21 00:08:42
【问题描述】:

从我的 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);
  }
  );
}

【问题讨论】:

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


    【解决方案1】:

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

    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');
        }
      }
    );
    

    这会立即安装应用程序,但安装后并没有重新打开。我要去解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      相关资源
      最近更新 更多