Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pass parameters to another app

Discussion in 'Scripting' started by jjuam, Aug 3, 2016.

  1. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    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!!
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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.
     
  3. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    Thanks for your answer!! Yes, I have the way to start app2 from app1. But, how can I get the arguments passed in app2?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you follow that link it shows you that the second parameter is the arguments to be passed
     
  5. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    I followed the link, and I know that second parameter is the arguments to be passed. I´ll try to explain better :)
    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 :( and thanks again!!
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    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.
     
  7. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In C# you can retrieve command line arguments by calling:
    Code (CSharp):
    1. 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.
     
  8. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    Thanks for the answers! Both apps are created in Unity. I´ll probe it!
     
  9. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    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:
    Code (CSharp):
    1. bool fail = false;
    2.         string bundleId = "com.one.two";
    3.         AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    4.         AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
    5.         AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
    6.        
    7.         AndroidJavaObject launchIntent = null;
    8.         try
    9.         {
    10.             launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",bundleId);
    11.         }
    12.         catch (System.Exception e)
    13.         {
    14.             fail = true;
    15.         }
    16.        
    17.         if (fail) { //open app in store
    18.             Application.OpenURL ("https://google.com");
    19.         } else //open the app
    20.             ca.Call("startActivity",launchIntent);
    21.        
    22.         up.Dispose();
    23.         ca.Dispose();
    24.         packageManager.Dispose();
    25.         launchIntent.Dispose();
    How can I pass args to app2? And how can I read those args like variables on app2?

    Thanks a lot!!
     
  10. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    Nobody can help me? I´m trying to pass args with Call method, but I can´t...

    Thanks again!!
     
  11. jjuam

    jjuam

    Joined:
    Jun 12, 2015
    Posts:
    51
    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:
    Code (CSharp):
    1. string arg1 = field.text.ToString ();
    2. launchIntent.Call<AndroidJavaObject("putExtra","arg1",arg1);
    3. ca.Call ("startActivity", launchIntent);
    Then in app2, I want to check if any argument has been passed in the first scene:
    Code (CSharp):
    1. void Start ()
    2.     {
    3. string arguments1 = "";
    4.        
    5.         AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    6.         AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    7.        
    8.         AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
    9.         bool hasExtra = intent.Call<bool> ("hasExtra", "arg1");
    10.  
    11.         if (hasExtra)
    12.         {
    13. AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras");
    14. 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!!
     
  12. neelgajjar

    neelgajjar

    Joined:
    Apr 3, 2017
    Posts:
    1
    hi jjum,
    you had used this code to pass a parameter to another app
    Code (CSharp):
    1. string arg1 = field.text.ToString ();
    2. launchIntent.Call<AndroidJavaObject("putExtra","arg1",arg1);
    3. 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.
     
  13. abibarte

    abibarte

    Joined:
    Feb 22, 2019
    Posts:
    3

    Some one have solution on IOS???