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>
  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>