Unity 纯C# 完成 APK从下载到 自安装
时间:2021-09-08
本文章向大家介绍Unity 纯C# 完成 APK从下载到 自安装,主要包括Unity 纯C# 完成 APK从下载到 自安装使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
最简单的就是用androidStudio 进行编辑,打个aar 包,在Unity中调用方法,很便捷
以下内容均转载Unity论坛,Android API24版本下可用,android API 24以上版本未进行测试
void Start() { StartCoroutine(downLoadFromServer()); } IEnumerator downLoadFromServer() { string url = "http://apkdl.androidapp.baidu.com/public/uploads/store_2/f/f/a/ffaca37aaaa481003d74725273c98122.apk?xcode=854e44a4b7e568a02e713d7b0af430a9136d9c32afca4339&filename=unity-remote-4.apk"; string savePath = Path.Combine(Application.persistentDataPath, "data"); savePath = Path.Combine(savePath, "AntiOvr.apk"); Dictionary<string, string> header = new Dictionary<string, string>(); string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; header.Add("User-Agent", userAgent); WWW www = new WWW(url, null, header); while (!www.isDone) { //Must yield below/wait for a frame GameObject.Find("TextDebug").GetComponent<Text>().text = "Stat: " + www.progress; yield return null; } byte[] yourBytes = www.bytes; GameObject.Find("TextDebug").GetComponent<Text>().text = "Done downloading. Size: " + yourBytes.Length; //Create Directory if it does not exist if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); GameObject.Find("TextDebug").GetComponent<Text>().text = "Created Dir"; } try { //Now Save it System.IO.File.WriteAllBytes(savePath, yourBytes); Debug.Log("Saved Data to: " + savePath.Replace("/", "\\")); GameObject.Find("TextDebug").GetComponent<Text>().text = "Saved Data"; } catch (Exception e) { Debug.LogWarning("Failed To Save Data to: " + savePath.Replace("/", "\\")); Debug.LogWarning("Error: " + e.Message); GameObject.Find("TextDebug").GetComponent<Text>().text = "Error Saving Data"; } //Install APK installApp(savePath); } public bool installApp(string apkPath) { try { AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent"); string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW"); int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW); AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath); AndroidJavaClass uriObj = new AndroidJavaClass("android.net.Uri"); AndroidJavaObject uri = uriObj.CallStatic<AndroidJavaObject>("fromFile", fileObj); intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive"); intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK); intent.Call<AndroidJavaObject>("setClassName", "com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity"); AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); currentActivity.Call("startActivity", intent); GameObject.Find("TextDebug").GetComponent<Text>().text = "Success"; return true; } catch (System.Exception e) { GameObject.Find("TextDebug").GetComponent<Text>().text = "Error: " + e.Message; return false; } }
第二种:对于Android API 24 以及更高版本 , 由于API的更改, 需要不同的代码
//For API 24 and above private bool installApp(string apkPath) { bool success = true; GameObject.Find("TextDebug").GetComponent<Text>().text = "Installing App"; try { //Get Activity then Context AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext"); //Get the package Name string packageName = unityContext.Call<string>("getPackageName"); string authority = packageName + ".fileprovider"; AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent"); string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW); int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK"); int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"); //File fileObj = new File(String pathname); AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath); //FileProvider object that will be used to call it static function AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider"); //getUriForFile(Context context, String authority, File file) AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj); intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive"); intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK); intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION); currentActivity.Call("startActivity", intent); GameObject.Find("TextDebug").GetComponent<Text>().text = "Success"; } catch (System.Exception e) { GameObject.Find("TextDebug").GetComponent<Text>().text = "Error: " + e.Message; success = false; } return success; }
原文地址:https://www.cnblogs.com/DGJS/p/15129584.html
- C#获取本机可用端口
- 想知道你的对象用微信和谁聊得最多吗?一个代码告诉你
- objective-C中的接口与泛型
- 段永朝:天性,互联网的灵性回归
- objective-C中的Class(类类型),Selector(选择器SEL),函数指针(IMP)
- 数据中心进水了怎么办?数据中心如何防洪?
- 戴若犁:虚拟中的现实
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(1)
- objective-C中如何判断一个类中有没有定义某个方法
- Steve Boswell:智能口罩让PM2.5滚蛋
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(2)
- objective-C中的扩展方法与partial class
- 仿优酷Android客户端图片左右滑动(自动滑动)
- objective-C: NSString应该用initWithFormat? 还是 stringWithFormat?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【C语言简单说】七:自定义函数(3)
- 【C语言简单说】八:分支结构之if(1)
- 【C语言简单说】八:分支结构之if...else...(2)
- 【C语言简单说】八:分支结构之if...else if()...else...(3)
- 【C语言简单说】九:输入
- 【C语言简单说】十:小结
- 【C语言简单说】十一:switch 补
- 【C语言简单说】十二:逻辑运算符&&
- 【C语言简单说】十三:逻辑运算符||
- 【C语言简单说】十三:变量的生命周期
- 【C语言简单说】十四:for循环
- 【C语言简单说】十五:while循环
- 【C语言简单说】十六:do...while循环
- 【C语言简单说】十七:数组
- 【C语言简单说】十七:数组(补)