Java 我使用Unity进行apk自动安装
我复制了一个源代码 但如果我在手机上玩,它会显示一个错误 错误:android.content.ActivityNotFoundException:未找到可处理Intent{act=android.Intent.action.VIEW dat的活动=content://com.mycompany.productname.fileprovider/files_root/files/data/ApkDownload.apk typ=application/vnd.android.package-archive flg=0x10000001} 代码:Java 我使用Unity进行apk自动安装,java,unity3d,apk,Java,Unity3d,Apk,我复制了一个源代码 但如果我在手机上玩,它会显示一个错误 错误:android.content.ActivityNotFoundException:未找到可处理Intent{act=android.Intent.action.VIEW dat的活动=content://com.mycompany.productname.fileprovider/files_root/files/data/ApkDownload.apk typ=application/vnd.android.package-a
public类AutoUpdateNew:monobhavior{
公共字符串下载URL;
void Start()
{
启动例程(从服务器()下载);
}
IEnumerator从服务器()下载
{
//downloadurl=字符串url;
字符串url=”https://drive.google.com/uc?export=download&id=1SUqRAITK-8ezVA7t6ORuuS8X9f69ei8v”;
字符串savePath=Path.Combine(Application.persistentDataPath,“data”);
savePath=Path.Combine(savePath,“ApkDownload.apk”);
字典头=新字典();
string userAgent=“Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/55.0.2883.87 Safari/537.36”;
header.Add(“用户代理”,userAgent);
WWW=新的WWW(url,空,标题);
而(!www.isDone)
{
//必须在下方屈服/等待一帧
GameObject.Find(“TextDebug”).GetComponent().text=“Stat:”+www.progress;
收益返回空;
}
byte[]yourBytes=www.bytes;
GameObject.Find(“TextDebug”).GetComponent().text=“下载完毕。大小:”+yourBytes.Length;
//创建不存在的目录
如果(!Directory.Exists(Path.GetDirectoryName(savePath)))
{
CreateDirectory(Path.GetDirectoryName(savePath));
GameObject.Find(“TextDebug”).GetComponent().text=“Created Dir”;
}
尝试
{
//现在保存它
System.IO.File.writealBytes(存储路径,yourBytes);
Debug.Log(“将数据保存到:“+savePath.Replace”(“/”,“\\”));
GameObject.Find(“TextDebug”).GetComponent().text=“保存的数据”;
}
捕获(例外e)
{
Debug.LogWarning(“未能将数据保存到:”+savePath.Replace(“/”,“\\”));
Debug.LogWarning(“错误:+e.Message”);
GameObject.Find(“TextDebug”).GetComponent().text=“保存数据时出错”;
}
//安装APK
installApp(保存路径);
}
/*
public bool installApp(字符串apkPath)
{
尝试
{
AndroidJavaClass intentObj=新的AndroidJavaClass(“android.content.Intent”);
字符串ACTION_VIEW=intentObj.GetStatic(“ACTION_VIEW”);
int FLAG_ACTIVITY_NEW_TASK=intentObj.GetStatic(“FLAG_ACTIVITY_NEW_TASK”);
AndroidJavaObject intent=新的AndroidJavaObject(“android.content.intent”,ACTION\u视图);
AndroidJavaObject fileObj=新的AndroidJavaObject(“java.io.File”,apkPath);
AndroidJavaClass uriObj=新的AndroidJavaClass(“android.net.Uri”);
AndroidJavaObject uri=uriObj.CallStatic(“fromFile”,fileObj);
调用(“setDataAndType”,uri,“application/vnd.android.package归档”);
调用(“addFlags”,标记活动和新任务);
调用(“setClassName”、“com.mycompany.productname”、“com.mycompany.productname.PackageInstallectivity”);
AndroidJavaClass unityPlayer=新的AndroidJavaClass(“com.unity3d.player.unityPlayer”);
AndroidJavaObject currentActivity=unityPlayer.GetStatic(“currentActivity”);
currentActivity.Call(“startActivity”,intent);
GameObject.Find(“TextDebug”).GetComponent().text=“Success”;
返回true;
}
捕获(System.e例外)
{
GameObject.Find(“TextDebug”).GetComponent().text=“Error:”+e.Message;
返回false;
}
}*/
私有bool installApp(字符串apkPath)
{
布尔成功=真;
GameObject.Find(“TextDebug”).GetComponent().text=“安装应用程序”;
尝试
{
//获取活动然后获取上下文
AndroidJavaClass unityPlayer=新的AndroidJavaClass(“com.unity3d.player.unityPlayer”);
AndroidJavaObject currentActivity=unityPlayer.GetStatic(“currentActivity”);
AndroidJavaObject unityContext=currentActivity.Call(“getApplicationContext”);
//获取包名
字符串packageName=unityContext.Call(“getPackageName”);
字符串权限=packageName+“.fileprovider”;
AndroidJavaClass intentObj=新的AndroidJavaClass(“android.content.Intent”);
字符串ACTION_VIEW=intentObj.GetStatic(“ACTION_VIEW”);
AndroidJavaObject intent=新的AndroidJavaObject(“android.content.intent”,ACTION\u视图);
int FLAG_ACTIVITY_NEW_TASK=intentObj.GetStatic(“FLAG_ACTIVITY_NEW_TASK”);
int FLAG_GRANT_READ_URI_PERMISSION=intentObj.GetStatic(“FLAG_GRANT_READ_URI_PERMISSION”);
//File fileObj=新文件(字符串路径名);
AndroidJavaObject fileObj=新的AndroidJavaObject(“java.io.File”,apkPath);
//用于调用静态函数的FileProvider对象
AndroidJavaClass fileProvider=新的AndroidJavaClass(“android.support.v4.content.fileProvider”);
//getUriForFile(上下文、字符串权限、文件)
AndroidJavaObject uri=fileProvider.CallStatic(“getUriForFile”,unityContext,authority,fileObj);
调用(“setDataAndType”,uri,“application/vnd.android.package归档”);
调用(“addFlags”,标记活动和新任务);
调用(“addFlags”,FLAG\u GRANT\u READ\u URI\u PERMISSION);
currentActivity.Call(“startActivity”,intent);
GameObject.Find(“TextDebug”).GetComponent().text=“Success”;
}
捕获(System.e例外)
{
GameObject.Find(“TextDebug”).GetComponent().text=“
public class AutoUpdateNew : MonoBehaviour {
public string downloadurl;
void Start()
{
StartCoroutine(downLoadFromServer());
}
IEnumerator downLoadFromServer()
{
//downloadurl = string url;
string url = "https://drive.google.com/uc?export=download&id=1SUqRAITK-8ezVA7t6ORuuS8X9f69ei8v";
string savePath = Path.Combine(Application.persistentDataPath, "data");
savePath = Path.Combine(savePath, "ApkDownload.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.mycompany.productname","com.mycompany.productname.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;
}
}*/
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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.productname" xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal" android:versionName="1.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.success.apkupdateTest.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
<activity android:name=".PackageInstallerActivity">
</activity>
</application>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
</manifest>