最简单的就是用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的更改, 需要不同的代码

推荐用Android打一个aar包  ,简单方便效率高

  

//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;
}

  

最新文章

  1. 用vue.js学习es6(一):基本工具及配置
  2. Photosohp 2017 已经发布!(下载地址及破解方法在文章底部)
  3. 安装go语言,配置环境及IDE,只需3步
  4. 44. log(n)求a的n次方[power(a,n)]
  5. HDU 2571 命运 (DP)
  6. PHP中的可变参数函数和可选参数函数
  7. php 地址跳转
  8. css margin重叠
  9. Android SDK三种更新失败及其解决方法
  10. 极路由1s,固件需要刷入RipOS系统的加40块
  11. WPF六个控制概述
  12. 在Android上实现SSL握手(客户端需要密钥和证书),实现服务器和客户端之间Socket交互
  13. spring security 3.x 多页面登录配置入门教程
  14. 【shell编程基础0】bash shell编程的基本配置
  15. 黑an网络---an网入门
  16. php 非常简单的导入sql文件
  17. C/C++练习题(二)
  18. Hadoop HBase概念学习系列之HFile(二十)
  19. 黑客编程教程(八)编写NT服务
  20. MYSQL IFNULL函数的使用

热门文章

  1. MySQL Galera cluster 集群常用参数说明
  2. Web服务器1-socket编程
  3. win10安装双版本mysql的方法
  4. apollo兼容mysql的时候报错Table &#39;apolloportaldb.hibernate_sequence&#39; doesn&#39;t exist
  5. 链接mysql数据库报错:2003-cant connect to Mysql server on ‘localhost’(10038)
  6. Flink Table API &amp; SQL 自定义Redis Sink 使用方式
  7. Win10桌面点鼠标右键一直转圈的解决方法
  8. 个人css样式_2: 渐变色
  9. pr导出mp4格式提示无法播放解决方案
  10. SQL Server 2012主从数据库的订阅和发布,实现数据库读写分离(主从备份)