• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by amartin94 · Oct 19, 2014 at 08:57 AM · c#androidjava

Launching an external android app in C#/Unity

So i've been stuck on this problem for literally days. I have integrated Unity with Eclipse IDE and i can build and deploy projects perfectly. However, im trying to start an a basic Intent on the java side and trigger it on the Unity/C# side.

Here's my code for the Java side:

 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;

 import com.unity3d.player.UnityPlayerNativeActivity;

 public class AppLauncher extends UnityPlayerNativeActivity 
 {

     public  Intent myIntent;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);

         //Assuming that we want to launch the browser to browse a website
         Uri uri = Uri.parse("http://www.google.com");
         myIntent= new Intent(Intent.ACTION_VIEW, uri);
     }

     public void Trigger()
     {       
         startActivity(myIntent);

     }
 }

Here's the error im getting thrown by logcat when the C# trigger is hit:

And here's my code for the C# side of if it:

 if(s[0].Equals("Spr"))
 {
     print("Launched");
     AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
     AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
     jo.Call("Trigger");
 }

Here's the error im getting thrown by logcat when the C# trigger is hit:

 AndroidJavaException: java.lang.NoSuchMethodError: no method with name='Trigger'     signature='()V' in class Lcom/unity3d/player/UnityPlayerNativeActivity;

I've tried screwing around by passong a custom signature along with the Trigger method name in the C# script, ive tried extending the standard UnityPlayerActivity, etc... I've tried hours worth of stuff and i Cannot seem to solve tis problem.

Any help is greatly accepted!


EDIT

This is the application launch section of the manifest:

 <application android:icon="@drawable/app_icon" android:label="@string/app_name" >
 <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
   <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
   <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
 </activity>
  
  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by liortal · Oct 19, 2014 at 09:09 AM

  1. Your Java code looks OK.

  2. Your C# code looks OK*

The only thing that's missing is you need to tell Unity that you have your own custom activity and it should use it.

In order to do that, you can follow the instructions in Building Plugins for Android, specifically the part under "Extending the UnityPlayerActivity Java Code".

Basically, you need to have an AndroidManifest.xml file that declares your own custom activity.

You copy the manifest to your project under Plugins/Android. Also, copy the compiled version of your Java code (.jar file) to Plugins/Android.

Follow the instructions in the link i provided to fully understand the process.

  • It is a safer practice to wrap usages of AndroidJavaClass and AndroidJavaObject in using { }

e.g:

 // Makes sure the AndroidJavaObject JNI resources get disposed after usage
  using (AndroidJavaObject ob = new .... ) 
 {
     // use android java object here 
 }


EDIT:

The modified AndroidManifest.xml should look something like this:

 <application android:icon="@drawable/app_icon" android:label="@string/app_name" >
  <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
    <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
  </activity>
  <activity android:name=".AppLauncher" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
  </activity>

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amartin94 · Oct 19, 2014 at 09:11 AM 0
Share

I should point out that ive integerated unity with eclipse, so i import all my assets from unity into eclipse and build from there: refer to http://forum.unity3d.com/threads/integrating-unity-and-eclipse.71607/

avatar image amartin94 · Oct 19, 2014 at 09:12 AM 0
Share

I thoughtthat meant i didnt need to do all that unity plugin crap ?

avatar image liortal · Oct 19, 2014 at 09:14 AM 0
Share

Well, you need to tell the Android manifest which activity is the main activity of your app. If you don't modify the Android$$anonymous$$anifest.xml, it will launch UnityPlayerActivity by default, and that activity does not have any Trigger() method on it.

avatar image amartin94 · Oct 19, 2014 at 09:20 AM 0
Share

what do i need to replace to launch my main activity? I will edit my main post and post the manifest in the edit

avatar image liortal · Oct 19, 2014 at 09:31 AM 0
Share

updated my answer. I haven't tested it but you can see that i add your own AppLauncher activity, and removed the intent filter (action.$$anonymous$$AIN) from Unity's default activity

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Java - Android - Accessing the /data/data folder 0 Answers

Multiple Cars not working 1 Answer

Faster way to Pass data from C# to Java (Android) 1 Answer

[ANDROID]Set Java Plugin on Unity 0 Answers

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges