Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. private bool InstallApp(string apkPath)
  2. {
  3. bool success = true;
  4. Debug.Log("InstallApp");
  5. progressStatus.text = "Installing Game...";
  6. try
  7. {
  8. //Get Activity then Context
  9. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  10. AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  11. AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
  12.  
  13. //Get the package Name
  14. string authority = "com.digient.skill4winlobby.preprod.fileprovider";
  15.  
  16. AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent");
  17. string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW");
  18. AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW);
  19.  
  20.  
  21. int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK");
  22. int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");
  23.  
  24. //File fileObj = new File(String pathname);
  25. AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
  26. //FileProvider object that will be used to call it static function
  27. AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider");
  28. //getUriForFile(Context context, String authority, File file)
  29. AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);
  30.  
  31. intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
  32. intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
  33. intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);
  34. currentActivity.Call("startActivity", intent);
  35. progressStatus.text = "Installed Game...";
  36. Debug.Log("Success");
  37. }
  38. catch (System.Exception e)
  39. {
  40. //This is the error line
  41. Debug.Log("Error "+ e.Message);
  42. quitGameButtonUpdate.SetActive(true);
  43. progressStatus.text = "Error Installing Game...";
  44. success = false;
  45. }
  46. progressStatus.text = "Success!...";
  47. return success;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement