16

I know this seems to be a trivial question but I could not find any concrete answer anywhere on the internet. I saw this very similar question on stackoverflow: How to start Unity application from android activity? but it is exactly opposite from my question. Additionally the android activity must be able to receive some input strings from the Unity application much like how one use system() calls with line arguments to start another program on a PC.

The following is the code I have for a test button event handler for my test Unity app on Android:

private void ExternalAppCallHandler()
{
    if(Application.platform == RuntimePlatform.WindowsEditor)
    {
        Process.Start(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
    }
    else if(Application.platform == RuntimePlatform.Android)
    {
        Process.Start("Internet");
    }
}

When I use Unity Editor to test, the application successfully opens Notepad++.exe when I click on the test button. However, when I tried to open the "Internet" app on my Samsung Galaxy S2 device it failed. Does anyone knows why this is the case? What should be the correct string for opening another Android application using Process.Start?

Community
  • 1
  • 1
ksming
  • 1,422
  • 1
  • 16
  • 26
  • I'm so sad no one can help me with this... – ksming Apr 12 '12 at 09:19
  • I have found two ways to go about doing this:1. Extend UnityPlayerActivity or 2. Create a Android Native plugin that starts another activity. I have tried method 1 many times but it always end up in forced-close situation. Hopefully method 2 will work for me. – ksming Apr 17 '12 at 09:34
  • I have a same question .Is possible that we call another android app in mobile within unity ? Such as audio recorder , video recorder , ... I want in my unity app call audio recorder and record sound of user. Is this possible? – hmahdavi Jan 29 '16 at 07:49

2 Answers2

10

I am not very familiar with Unity, but have a fair amount of Android experience. So take my answer as a suggestion rather than an authoritative answer.

Looking at the Launching an Android application from within Unity, you could try the following:

Follow the Guide on Integrating Unity with Eclipse.

Modify the Java file created in Step 1 as below:

package com.Unity3D.EclipseIntegration;

import android.os.Bundle;

import com.unity3d.player.UnityPlayerActivity;

public class EclipseIntegration extends UnityPlayerActivity {

    private 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 Launch()
    {       
        startActivity(myIntent);
    }
}

and modify your Unity code:

private void ExternalAppCallHandler()
{
    if(Application.platform == RuntimePlatform.WindowsEditor)
    {
        Process.Start(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
    }
    else if(Application.platform == RuntimePlatform.Android)
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("Launch");
    }
}

In case you are facing any problems, please post the LogCat messages.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Actually, I have tried something like what you have suggested but without using Eclipse integration. It's from the example in http://unity3d.com/support/documentation/Manual/PluginsForAndroid.html under the Extending the UnityPlayerActivity Java Code section. Tried compiling with Javac directly and adding extended class with classes.jar to create a Extended.jar. I even reached the stage where I can build the apk file successfully but when I run it on Android device, forced-close occurs. Have been pulling my hair trying to figure out what is wrong with the input AndroidManifest.xml file. – ksming Apr 20 '12 at 09:41
  • Would it be a lot of work to give the Eclipse integration a try? As I said, I am not familiar with the Unity framework, so it would be difficult for me to understand what problems you might be facing. Also, if possible, paste the LogCat messages for your force-closes. – Rajesh Apr 20 '12 at 10:44
  • Rajesh: I will mark your answer as correct as they seem closest to what I want. – ksming Apr 23 '12 at 09:42
8

try this Change this Launch() method to static and pass Android java object ie. "jo" to it like below.

AndroidJavaClass androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo= androidJC.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass jc = new AndroidJavaClass("package_name.Ur_Actvity_Name");
jc.CallStatic("Launch",jo);`

and change Launch() method to :

public static Launch(Activity activity)
{
 Intent myIntent = new Intent();
 activity.startActivity(myIntent);
}

Hope it will help.

amartin94
  • 505
  • 3
  • 19
  • 35
Ashish Dhore
  • 211
  • 4
  • 11