资源都下载好后开始检查是否需要覆盖安装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;
}
发包,安装到模拟器上: