Search Unity

Android Plugin. multiple Android Activity problems.

Discussion in 'Android' started by yu_2, Aug 28, 2012.

  1. yu_2

    yu_2

    Joined:
    Aug 6, 2012
    Posts:
    4
    I made 2 plugins, but both have Activty.
    Activity does not move only to one at the same time, I want to change the activity of these on unity.

    --------------------------------------------------

    // my code
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" );
    AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>( "currentActivity" );

    // my AndroidManifest.xml
    <activity
    android:name="com.hoge.prj.HogeActivity"
    android:label="@string/app_name"
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <activity android:name="com.geho.prj.GehoActivity"
    android:label="@string/app_name">
    </activity>

    --------------------------------------------------

    This code, as well as it was a class that inherits the UnityPlayer,

    Wrote that "android.intent.action.MAIN" using the tag in the manifest intent becomes the priority.

    I want to switch the activity on unity.
    Should we write in Java code?

    I want your advice.
    Thank you :>
     
    Last edited: Aug 28, 2012
  2. Psykna

    Psykna

    Joined:
    Oct 11, 2012
    Posts:
    2
    Hi,

    I have exactly the same problem. I have 2 android plugins, Vuforia and my plugin, but only one activity can be the "currentActivity" of Unity:

    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

    -> In the manifest, if the activity declared as "android.intent.action.MAIN" is the Vuforia one then I cannot launch my activities, and vice versa, if my root activity is the MAIN, then Vuforia cannot be launched.

    I wonder how 2 main activities can co-exist or how to launch two activities from different packages.

    Does anyone have found a solution?
    Thanks for your help
     
  3. Psykna

    Psykna

    Joined:
    Oct 11, 2012
    Posts:
    2
    I found a solution on my side : grab the current active activity (can be an other android plugin or your own activity) and start statically a new activity from it:

    In the .cs file:

    Code (csharp):
    1. #if UNITY_ANDROID
    2.    //Grab the current activity (the one declared as MAIN in the manifest - can be an other plugin)
    3.    AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    4.    AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");
    5.  
    6.    //Give the activity instance to my own static method to launch an activity from there
    7.    var jc = new AndroidJavaClass("com.my.package.RootActivity");
    8.    jc.CallStatic("launchActivity", ajo, showUI, true, "string", 0);
    9. #endif

    In the com.my.package.RootActivity java file :

    Code (csharp):
    1. public static void launchActivity(Activity root, boolean param1, String param2, int param3) {
    2.     Intent intent = new Intent(root, CameraActivity.class);
    3.     intent.putExtra(RootActivity.EXTRA_BOOLEAN_PARAM1, param1);
    4.     intent.putExtra(RootActivity.EXTRA_STRING_PARAM2, param2);
    5.     intent.putExtra(RootActivity.EXTRA_INT_PARAM3, param3);
    6.     root.startActivity(intent);
    7. }
    Hope it helps ;)
     
  4. ant001

    ant001

    Joined:
    Dec 15, 2010
    Posts:
    116
    thanks Psykna
     
  5. Santy

    Santy

    Joined:
    Apr 16, 2013
    Posts:
    26
    Could you explain more the solution please? Thank you!!
     
  6. Engidia_EduardBosch

    Engidia_EduardBosch

    Joined:
    Nov 14, 2013
    Posts:
    25
  7. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Thanks Psykna! Your solution worked for me as well!
     
  8. Alex_Brazil

    Alex_Brazil

    Joined:
    Sep 2, 2014
    Posts:
    6
    Hello,

    Use Vuforia with AdMobe (by Google). Both require the declaration of a main activity in the manifest and the two do not fear coexist. I am a beginner programmer and do not know how to extend the plugin AdMobe with Vuforia. Can someone help me?
     
  9. DontShooot

    DontShooot

    Joined:
    Nov 27, 2012
    Posts:
    4
    If I understood it right - I must have access to another plugin sources to launch new activity, right?
     
  10. Northmaen

    Northmaen

    Joined:
    Jun 23, 2015
    Posts:
    17
    Hi Psykna,

    My problem seems to be the same as your (Except i use Metaio and a custom NFC plugin). So can you explain more in depth how your plugin works? I understand the C# code but I don't understand the java part. Should I put this chunk of code in my own Eclipse Project and recompile it in a new .jar?

    Thanks!
     
  11. masterchop

    masterchop

    Joined:
    Oct 9, 2015
    Posts:
    39
    What is the difference between Call and CallStatic? i have an android plug in developed by me.
    And works fine when i use alone, i have another project which runs Vuforia, Cardboard and leap motion plugins all working fine with no change. Why they work fine and mine is not?
    I need to understand how this works?
    is is possible to make the plugin with no activity? just services or classes? so we call them form Unity?
    or is mandatory to have an activity? i just couldnt find a post that explains the plugins stuff.
     
  12. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    Exactly, I was thinking about just the same, and you're absolutely right. There is no need to subclass main activity, nor include a single AndroidManifest.xml at all. Android plugins can be wrapped in `Fragment` classes as well (kind of sub-activities). They spare the stuff mentioned above, while you can still start and receive results from native Activities. Also it provides a single instance for Unity, so C# code can invoke methods on the singleton directly.

    I spend days on research / prototype this, gonna publish an exhaustive tutorial on the subject.

    I think people should stop subclassing UnityPlayerActivity altogether, to avoid situations like Unity3D plugins multiple MAIN activities in Android manifest file (so the original question).
     
    masterchop likes this.
  13. masterchop

    masterchop

    Joined:
    Oct 9, 2015
    Posts:
    39
    Cant wait for your tutorial!
     
    eppz likes this.
  14. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    Wow, it has even more details since than, with project setup, single folder plugin (great for git submodules / to be able work on the plugin in client projects), and more. Now I have to apply it in a production production project, but I'm planning to do the tuts on the weekend, along with an example github repo.
     
  15. JHSV

    JHSV

    Joined:
    Jul 11, 2013
    Posts:
    26
    eppz, thank you for making that tutorial. Do you have it ready? If so, could you please post the link here? I'm trying to follow the examples above. In particular, Psykna's post on Oct 12, 2012.

    I am now using Unity 5.3.3p3.

    Psykna, thank you for providing that solution. For some reason, the Unity Android app cannot recognize the class I am including in this line in place of CameraActivity.class in the Java file:

    Intent intent =new Intent(root, MyActivity.class);​

    I just keep getting this error:

    04-13 18:52:52.218 18259 18294 I Unity : java.lang.NoClassDefFoundError: com.example.myapp.MyActivity​

    Also, for this line, I used a custom class not inherited from Activity instead of the custom RootActivity, although inheriting it from Activity did not fix it either:

    var jc =newAndroidJavaClass("com.my.package.RootActivity");​

    I use Android Studio 2.0 for the Android development, and I see that the MyActivity.class is included in the jar file. It is also defined in my AndroidManifest.xml in Plugins/Android.

    <activityandroid:name="com.example.myapp.MyActivity">
    <intent-filter>
    <actionandroid:name="com.example.myapp.action.VIEW"/>

    <categoryandroid:name="android.intent.category.DEFAULT"/>

    <dataandroid:scheme="http"/>
    <dataandroid:scheme="https"/>
    <dataandroid:scheme="content"/>
    <dataandroid:scheme="asset"/>
    <dataandroid:scheme="file"/>
    </intent-filter>
    </activity>


    Could someone please help me troubleshoot this issue? Thank you.
     
  16. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    Last edited: Apr 27, 2016
  17. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    531
    @eppz you are a God for doing this!
     
    eppz likes this.
  18. DioArvin

    DioArvin

    Joined:
    Oct 29, 2013
    Posts:
    2
    @Psykna
    Thanks Psykna~
    Your solution worked for me as well~:D
     
  19. masterchop

    masterchop

    Joined:
    Oct 9, 2015
    Posts:
    39
    Cant wait to test this out, than kyou EPPZ !!
     
  20. Rainking

    Rainking

    Joined:
    Jun 10, 2013
    Posts:
    41
    Hi,
    I got a very annoying issue. All of my Android plugins in Unity just work if I dont use parameters in the calling functions. I am working on this issue since 2 days and I am about to give up. Even a minimal project doesnt work. I am sure I doing it right because the calling functions are working fine without parameter.

    Unity:
    AndroidJavaClass pluginClass = new AndroidJavaClass("tnbrtrm.lications.de.myplugin.MyPlugin");
    String test = pluginClass.CallStatic<string>("getMessage", new object[] { "alalalalala" });
    Debug.Log("callShareAppAARPlugin: " + test);

    Android:
    public class MyPlugin extends UnityPlayerActivity {

    public static String getMessage(String text)
    { return "Hello World! " + text; }
    }

    The plugin prints "Hello world" when I delete the parameter "String text", but with text I get this annoying error:

    AndroidJavaException: java.lang.NoSuchMethodError: no static method "Ltnbrtrm/lications/de/myplugin/MyPlugin;.getMessage(Ljava/lang/String;)Ljava/lang/String;"

    I see this error using Plugins as *.jar or *.aar. And I also had a look into this packages and my Classes and Functions are there. Otherwise it wouldnt work without parameters...

    Can anyone please help me? I tried a lot of things... (older JDK versions, etc.)
    Kind regards,
    Tino
     
    Last edited: Jul 26, 2016