Search Unity

Help reading the command line arguments in Unity

Discussion in 'Android' started by hoihoi8, Jun 4, 2015.

  1. hoihoi8

    hoihoi8

    Joined:
    May 1, 2015
    Posts:
    6
    Hello,
    Any suggestions? I'm having trouble getting command line arguments out of my Android app. I am starting my Unity app with

    Code (CSharp):
    1. ${ADBPATH} -s $DeviceId shell monkey -p my.app -c android.intent.category.LAUNCHER 1 -e automation true

    In my Unity on startup I have
    Code (CSharp):
    1. string extraIntentText = getAndroidArguments();
    2.  
    3.             if(extraIntentText.Contains("automation") && extraIntentText.Contains("true"))
    4.  
    5.             {
    6.  
    7.                 //start my automation
    8.             }
    Code (CSharp):
    1. private string getAndroidArguments()
    2.  
    3.         {
    4.  
    5.             string extraText = "";
    6.  
    7.             using (AndroidJavaClass unity_player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    8.  
    9.             {
    10.  
    11.                 var currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    12.  
    13.                 var intent = currentActivity.Call<AndroidJavaObject>("getIntent");
    14.  
    15.                 var type = intent.Call<string>("getType");
    16.  
    17.  
    18.  
    19.                 if (type == "text/plain") {
    20.  
    21.                     extraText = intent.Call<string>("getStringExtra", "android.intent.extra.TEXT" );
    22.  
    23.                 }
    24.  
    25.             }
    26.  
    27.            
    28.  
    29.             return extraText;
    30.  
    31.         }
     
    Last edited: Jun 4, 2015
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    From what you posted, it looks like you are running the Android "monkey" runner.
    By looking at the docs, I don't see any option that you can pass extras like you showed (with the -e switch): http://developer.android.com/tools/help/monkey.html

    Are you sure that this option is valid?

    Regarding your C# code, you are getting the Intent object, but what are you returning? in the command line, you are passing a single extra. you should probably check for its existence and return it..
     
  3. hoihoi8

    hoihoi8

    Joined:
    May 1, 2015
    Posts:
    6
    Hi,
    You were correct. Monkey Runner didn't accept command line arguments. I've changed my launch command to

    Code (CSharp):
    1. ${ADBPATH} -s $DeviceId shell am start my.app/my.activity -e automation true
    I'm still working on getting the C# code working.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    This is better. This command means you are launching your game (the activity you specified) and pass in an extra called automation with a value of true. I believe that if you only specify -e, the value is passed as a string (see the docs here: http://www.phonesdevelopers.com/1780676/)

    I think the command you should invoke is this:

    ${ADBPATH} -s $DeviceId shell am start my.app/my.activity --ez automation true

    this will pass the value as a boolean.

    Now for the C# code - your code attempts to grab all the arguments as a single string. I don't think that is possible. The "extras" you pass into the Intent are more like a dictionary of key-value. You should be checking for the existence of the "automation" key, with a value of true:
    Code (CSharp):
    1. private bool GetBoolValue(string extraKey, bool defaultValue)
    2. {
    3.     using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    4.     {
    5.         using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity"))
    6.         {
    7.             using (var intent = activity.Call<AndroidJavaObject>("getIntent"))
    8.             {
    9.                 return intent.Call<bool>("getBooleanExtra", extraKey, defaultValue);
    10.             }          
    11.         }
    12.     }
    13. }
    Then you could use this code to detect if "automation" was passed as true:
    Code (CSharp):
    1. if (GetBoolValue("automation", false))
    2. {
    3.     // Logic here
    4. }
    This is just from the top of my head, i didn't test this. Let me know if that worked.