Pass parameters to another app

Hi everybody!

I just created a scrit to open app2 from app1, and I´m wondering how can I pass parameters to app2. Besides, I would like to know how can app2 know if app1 launched it. I need to know if app2 is open by app1, because the app2 behavior have to be different in that case.

Thanks a lot!!

Use Process.Start(string, string) to execute another program with arguments. You can then use an argument to specify that app2 was executed by app1.

Thanks for your answer!! Yes, I have the way to start app2 from app1. But, how can I get the arguments passed in app2?

if you follow that link it shows you that the second parameter is the arguments to be passed

I followed the link, and I know that second parameter is the arguments to be passed. I´ll try to explain better :slight_smile:
I have 2 unity android apps. I´ve made that app1 can open app2 using AndroidJavaObject. I want to pass arguments between both app, and depending on arguments I want execute app2 different. In the link:
Process.Start(“IExplore.exe”, “www.northwindtraders.com”); open IExplorer on page www.northwindtraders, but that isn´t what I looking for. It´s more something like this:
App1
Process.Start(“App2”, variable); or androidJavaObject.call(“startActivity”,launchIntent,variable);
App2
get or read variable value passed by app1

Sorry about my explanations :frowning: and thanks again!!

This isn’t really a Unity question?

Depending on how you’re creating App2, the variable will be passed in somehow. In a “standard” C# or Java program, it’s the args array passed in the Main/main methods.

In C# you can retrieve command line arguments by calling:

string[] args = Environment.GetCommandLineArgs();

So if you send arguments via the way jimroberts described, I assume above line of code should give them to you, and you can do with them whatever you want.

Thanks for the answers! Both apps are created in Unity. I´ll probe it!

I can´t open app2 from app1 with Process.Start, because both apps are Android Unity apps. The way what I launch app2 from app1 is:

bool fail = false;
        string bundleId = "com.one.two"; 
        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);
        }
        catch (System.Exception e)
        {
            fail = true;
        }
       
        if (fail) { //open app in store
            Application.OpenURL ("https://google.com");
        } else //open the app
            ca.Call("startActivity",launchIntent);
       
        up.Dispose();
        ca.Dispose();
        packageManager.Dispose();
        launchIntent.Dispose();

How can I pass args to app2? And how can I read those args like variables on app2?

Thanks a lot!!

Nobody can help me? I´m trying to pass args with Call method, but I can´t…

Thanks again!!

Finally I got the solution. If anybody has the same problem, this is what I do to solve it:

In app1, which launch app2, I created a Input text field where I put the value I want to pass to app2. I add to the app1 launch code:

string arg1 = field.text.ToString ();
launchIntent.Call<AndroidJavaObject("putExtra","arg1",arg1);
ca.Call ("startActivity", launchIntent);

Then in app2, I want to check if any argument has been passed in the first scene:

void Start ()
    {
string arguments1 = "";
       
        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", "arg1");

        if (hasExtra)
        {
AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras");
arguments1 = extras.Call<string> ("getString", "arg1");

Now I have the arguments passed by app1 in app2 like variables : )
I hope this can be useful for somebody.
Thanks for the anwers!!

6 Likes

hi jjum,
you had used this code to pass a parameter to another app

string arg1 = field.text.ToString ();
launchIntent.Call<AndroidJavaObject("putExtra","arg1",arg1);
ca.Call ("startActivity", launchIntent);

In my case, i want to open the app with bundle id & with the extra parameter.

How to do that? Please help.
Thank you.

Some one have solution on IOS???