安卓覆盖安装

资源都下载好后开始检查是否需要覆盖安装apk,如果需要覆盖安装,先把Application.streamingAssetsPath的文件全部拷贝到Application.persistentDataPath做备份,如果要安装的apk文件存在直接调用安装:

public bool InstallAPK(string path, bool bReTry) { 

 try { 

 var Intent = new AndroidJavaClass("android.content.Intent"); 

 var ACTION_VIEW = Intent.GetStatic("ACTION_VIEW"); 

 var FLAG_ACTIVITY_NEW_TASK = Intent.GetStatic("FLAG_ACTIVITY_NEW_TASK");

 var intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW); 

 var file = new AndroidJavaObject("java.io.File", path); 

 var Uri = new AndroidJavaClass("android.net.Uri"); 

 var uri = Uri.CallStatic("fromFile", file);

 intent.Call("setDataAndType", uri, "application/vnd.android.package-archive"); 

 if (!bReTry) { 

 intent.Call("addFlags", FLAG_ACTIVITY_NEW_TASK);

 intent.Call("setClassName", "com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity"); 

 } 

 var UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 

 var currentActivity = UnityPlayer.GetStatic("currentActivity");

 currentActivity.Call("startActivity", intent);

 Debug.Log("Install New Apk Ok");

 return true;

}catch (System.Exception e){

            Debug.LogError("Error Install APK:" + e.Message + " -- " + e.StackTrace + "  bRetry=" + bReTry);

            return false;

        }

}

可以用模拟器(夜神模拟器、雷电模拟器等)测试一下看看是否调用成功,只是为了测试是否调用安卓成功那些资源下载版本对比什么的就不考虑了,直接出个apk包放在Application.streamingAssetsPath里打包再出个apk包,在启动代码那里添加测试逻辑,把Application.streamingAssetsPath里面的apk拷到Application.persistentDataPath最后直接调用上面的方法即可。

private IEnumerator TestInstallAPK()

    {

        string file = SteamingAssetsPath("Apk/ClientUpdate.apk");

        using (WWW www = new WWW(file))

        {

            yield return www;

            if (!string.IsNullOrEmpty(www.error))

            {

                GCDDebug.Log(www.error);

                yield break;

            }

            file = GetPersistentPath("Apk/ClientUpdate.apk");

            SaveBytesToFile(file, www.bytes);

            www.Dispose();

        }

        if (File.Exists(file))

        {

            GChildWindow messageBoxWindow = Super.ShowMessageBox(Super.MainWindowRoot, 0, Global.GetLang("提示"),             Global.GetLang("游戏资源已经更新,请安装最新的游戏包后重新进入游戏!"));

            messageBoxWindow.ChildWindowClose = (s1, e1) =>

            {

                Super.CloseMessageBox(Super.MainWindowRoot, messageBoxWindow);

                InstallAPK(file,true);

                Application.Quit();

                return true;

            };

        }

        else{

            Super.ShowMessageBox(Super.MainWindowRoot, 0, Global.GetLang("不存在"), file);

        yield return null;

    }

public void SteamingAssetsPath(string path = "")

{

string tmp = "";

if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer)

{

tmp = "file:///" + Application.dataPath + "/StreamingAssets/" + path;

}

else if(Application.platform == RuntimePlatform.Android)

{

tmp = Application.streamingAssetsPath + "/" + path;

}

else

{

   tmp = "file:///" + Application.streamingAssetsPath + "/" + path;

}

    return tmp;

}

public void GetPersistentPath(string path = "")

{

string tmp = Application.persistentDataPath + "/" + path;

return tmp;

}

public  bool  SaveBytesToFile(string path, byte[] bytes)

{

            try

            {

                if (File.Exists(path))

                {

                    File.Delete(path);

                }

                int index = path.LastIndexOf('/');

                if (-1 != index)

                {

                    Directory.CreateDirectory(path.Substring(0, index));

                }

                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))

                {

                    fs.Write(bytes, 0, bytes.Length);

                    fs.Flush();

                    fs.Close();

                    fs.Dispose();

                }

                return true;

            }

            catch (System.Exception ex)

            {

                GCDDebug.LogException(ex);

            }

            return false;

}

发包,安装到模拟器上:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,908评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,183评论 19 139
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,202评论 0 2
  • 世间所有的信仰都是引人走向爱、慈悲、和平与和谐,也只有这样的信仰才是真信仰和正信! 基督透过他的爱与...
    道花香飘阅读 4,468评论 0 0
  • 今天和朋友聊天,一个问题让我沉默了。 “你对你的未来有规划吗?” 我心里的答案是“没有。” 我只知道我要考南京大学...
    Megan雅心阅读 2,999评论 0 0

友情链接更多精彩内容