- Home /
Opening another Unity app with java intent, sending a parameter and reading it in the second Unity app
Hello everyone, I have been struggling with this problem for days now, I would really appreciate some help. I am making a gear vr game that requires people to put in their email and name before they play. The idea here is to avoid having a vr keyboard input and just have a simple launch app with UI fields that once filled will open the vr app and send the name and email as a java intent. I've been all over the web trying to look for solutions but nothing has worked so far, here is the code I use in the launch app:
public void LaunchApp() {
bool fail = false;
string message = playerName.text + "_" + email.text;
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
try
{
launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
launchIntent.Call<AndroidJavaObject>("putExtra", bundleId + "arguments", message);
}
catch (System.Exception e){
fail = true;
}
if (fail) {
Debug.Log("app not found");
}
else
{
ca.Call("startActivity", launchIntent);
}
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
}
and this is the code I use to try and read the intent in the vr app once it opens, right in the start function:
void Start () {
string arguments = "";
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
bool hasExtra = intent.Call<bool>("hasExtra", "arguments");
if (hasExtra)
{
AndroidJavaObject extras = intent.Call<AndroidJavaObject>("getExtras");
arguments = extras.Call<string>("getString", "arguments");
mytext.text = arguments;
}
}
I also tried going the "custom" android plugin way but no luck, I'm not that well versed in native java dev, Any ideas what I am doing wrong?
Thank you
I'm stuck on something similar myself, but co$$anonymous$$g from and android app. I'm not sure what the procedure for reading an intent is from unity. Was anyone able to get this working?
Answer by Onizuka101 · Oct 16, 2016 at 05:32 AM
UPDATE: So I feel like an idiot but I actually solved this... at the end all of the code was correct my only problem was that when sending the extra I included the bundle id for some reason. I removed it and it worked like a charm! I hope this will help anyone that comes across this problem.
Answer by angusmf · Jan 14, 2017 at 03:19 PM
As indicated in @Onizuka101's answer, the bundle id is added to the string identifier for the extra info. For completeness, the LaunchApp code should be:
public void LaunchApp() {
bool fail = false;
string message = playerName.text + "_" + email.text;
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
try
{
launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
launchIntent.Call<AndroidJavaObject>("putExtra", "arguments", message);
}
catch (System.Exception e){
fail = true;
}
if (fail) {
Debug.Log("app not found");
}
else
{
ca.Call("startActivity", launchIntent);
}
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
}
This is the important line of code.
launchIntent.Call("putExtra", "arguments", message);