1. 程式人生 > >apicloud 外部應用狀態 及app 下載並安裝外部應用

apicloud 外部應用狀態 及app 下載並安裝外部應用

            //app狀態 顯示不同的文字
            gameStatus:function(){
              var _self = this;
              var packageName = 'com.youku.phone'; //_self.gameData.snsGameAndroidName
              var savePath = "fs://res/downloadGame/"+packageName+".apk";
              //判斷是否安裝
              api.appInstalled({
                  appBundle: packageName
              }, function(ret, err) {
                  if (ret.installed) {
                    console.log("-----------已經安裝  開啟遊戲");
                    _self.downloadStatus = "開啟遊戲";

                  } else {
                    //沒有安裝 需要下載
                    console.log("-----------沒有安裝 判斷是否下載");
                    var fs = api.require('fs');
                    fs.exist({
                        path: savePath
                    }, function(ret, err) {
                        if (ret.exist) {
                            //已經下載 並未安裝
                            _self.downloadStatus = "開始安裝";
                        } else {
                            //未下載
                            _self.downloadStatus = "下載安裝";
                        }
                    });

                  }
              });
            },
            //外部APP下載或安裝或開啟
doClick:function(url,packageName){ var _self = this; var packageName = 'com.youku.phone'; var savePath = "fs://res/downloadGame/"+packageName+".apk"; var url = "http://gdown.baidu.com/data/wisegame/b4a968567a13edf3/youku_155.apk"; api.appInstalled({ appBundle: packageName }, function(ret, err) { if (ret.installed) { //已經安裝 開啟 console.log("-----------已經安裝 開啟"); api.openApp({ androidPkg: packageName, mimeType: 'text/html', uri: 'http://www.baidu.com' }, function(ret, err) { if (ret) { mytoast("開啟成功","middle"); } else { mytoast("開啟APP錯誤:" + err.msg, 'middle'); } }); } else { //沒有安裝 需要下載 console.log("沒有安裝 需要下載"); api.download({ url: url, savePath: savePath, report: true, cache: true, allowResume: true },function(ret, err){ if(ret.state == 1){ console.log("下載成功"); api.installApp({ appUri: savePath }); _self.downloadStatus = "開啟遊戲"; }else if(ret.state == 0){ console.log("---下載中---:"+ret.percent); _self.downloadStatus = "下載中"; var downloadBtn = $api.byId("downloadBtn"); $api.css(downloadBtn, 'background:linear-gradient(to right, #059CFA, #059CFA ' + ret.percent + '%,#ffffff ' + ret.percent + '%,#ffffff)'); }else if(ret.state == 2){ console.log("下載失敗!") } }); }