Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private bool InstallApp(string apkPath)
- {
- bool success = true;
- Debug.Log("InstallApp");
- progressStatus.text = "Installing Game...";
- 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 authority = "com.digient.skill4winlobby.preprod.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);
- progressStatus.text = "Installed Game...";
- Debug.Log("Success");
- }
- catch (System.Exception e)
- {
- //This is the error line
- Debug.Log("Error "+ e.Message);
- quitGameButtonUpdate.SetActive(true);
- progressStatus.text = "Error Installing Game...";
- success = false;
- }
- progressStatus.text = "Success!...";
- return success;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement