Launch another unity app from unity app param problem

Hello,

I have two unity application the first application launch the second app with this code:
Founded here : c# - Read Android intent extra data on Unity app launch - Stack Overflow
and here
https://community.unity.com/t5/Scripting/An-Unity-App-playing-another-Unity-app/m-p/2646073

public void LaunchIntent()
    {
     
        bool fail = false;
        string bundleId = "com.ravr.media"; // your target bundle id
        AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");

        AndroidJavaObject launchIntent = null;
        try
        {
            launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
        }
        catch (System.Exception e)
        {
            fail = true;
        }

        if (fail)
        { //open app in store
            Application.OpenURL("https://google.com");
        }
        else //open the app
        {
            launchIntent.Call<AndroidJavaObject>("setAction", "com.ravr.media.do");
            launchIntent.Call<AndroidJavaObject>("setType", "text/plain");
            launchIntent.Call<AndroidJavaObject>("putExtra", "KEY", "xxx@me.com, 235463, Male, 43, France, 13011, Leisure, 0");

            ca.Call("startActivity", launchIntent);
        }

        up.Dispose();
        ca.Dispose();
        packageManager.Dispose();
        launchIntent.Dispose();
    }
    }

On my second unity app I have created a plugin with this class (MainActivity.java):

package com.ravr.media;

import android.content.Intent;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleNewIntent(intent);
    }

    private void handleNewIntent(Intent intent){
        String text = intent.getStringExtra("KEY");
        UnityPlayer.UnitySendMessage("AccessManager","OnAccessToken", text);
    }
}

With this manifest : (in android studio)

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ravr.media">
    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Now here is the android manifest in unity :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.ravr.media"
      android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="21" />
  <application android:label="@string/app_name">
    <activity android:name=".MainActivity" android:label="@string/app_name" tools:merge="override">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
        <action android:name="com.ravr.media.do" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain"/>
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.GET_TASKS" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

Finaly I use this class in unity :

public class AccessManager : MonoBehaviour {
    public void OnAccessToken(string accessToken)
    {
        //string[] infos = Regex.Split(accessToken, ",");
        Debug.Log("Message Received!!!! :" + accessToken);
    }
}

The first app launch the second app correctly but params never receipt

In a standard android application I can use :

public void LaunchUnityApp(){
    Intent i=new Intent();
    i.setAction("com.company.plugin.do");
    i.setType("text/plain");
    i.putExtra("KEY","This is the text message sent from Android");
    startActivity(i);
}

Thanks for help

A few questions:

  1. Why are you sending the message from Android → Unity in onNewIntent ?

From the documentation here: Activity  |  Android Developers

Is your game activity set to singleTop or did you set the FLAG_ACTIVITY_SINGLE_TOP ? if not, i don’t think this will be called.

You should implement the onCreate method and add your code there.

  1. Calling SendMessage, but the game object may not even exist yet…

When the activity gets created, Unity did not load (or did not complete loading) the engine, load the first scene, etc.
There’s probably no game object by that name waiting to receive the message.

The logic should be turned around – when the game loads, a game object should query for those parameters by calling into Java. This ensures it’s available, instead of relying on the scene to load and that object to be available for receiving the message.

Hi liortal,

Thanks for reply, for first question, I’v just folowing the tutorial, and I’m not confident with android native function…
How do you set FLAG_ACTIVITY_SINGLE_TOP ? on manifest ? in the plugin ?

Second question, I understand for sendmessage and gameobject not loaded, again my bad, I just following tutorial…
I don’t find a good solution, all tutorial are for launch an app from unity but without parameter…

After some search here is code to send text from app1 (unity app):

public class ShareApp : MonoBehaviour {
 
string subject = "WORD-O-MAZE";
string body = "GAME LINK HERE + DESCRIPTION";
public void shareText(){
//execute the below lines if being run on a Android device
#if UNITY_ANDROID
  //Refernece of AndroidJavaClass class for intent
  AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent");
  //Refernece of AndroidJavaObject class for intent
  AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent");
  //call setAction method of the Intent object created
  intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
  //set the type of sharing that is happening
  intentObject.Call<AndroidJavaObject>("setType", "text/plain");
  //add data to be passed to the other activity i.e., the data to be sent
  intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
  intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body);
  //get the current activity
  AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
  AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
  //start the activity by sending the intent data
  currentActivity.Call ("startActivity", intentObject);
#endif
  
}
 
}

Now how to open app2 (unity app too) and get the string ?
Realy imossible to find some guidance on google

Tested some other case, in my first app I created plugin :

public static void LauchNewAppWithParam(String message){
        final PackageManager pm = UnityPlayer.currentActivity.getPackageManager();
        Intent LaunchIntent = pm.getLaunchIntentForPackage("com.ravr.media");

        try
        {
            LaunchIntent.putExtra("KEY", message);
            UnityPlayer.currentActivity.startActivity(LaunchIntent);
        }
        catch(Exception e)
        {

        }
    }

and I call this function in unity to open second app :

var ajc = new AndroidJavaClass("com.ravr.login.MainActivity");
        ajc.CallStatic("LauchNewAppWithParam", "xxxx@me.com, 35617, Male, 43, France, 13011, Leisure, 0");

Second application is opened fine, but now how I can get the value of “putExtra” in the second opened app?

Thanks

Anayone have a solution or guidance ? it’s a pain to find a solution for this

Hi,
Following worked for me:-

Thanks,

using UnityEngine;
using System.Collections;


public class ShareApp : MonoBehaviour
{
    string[] email = {"me@gmail.com"};
    string subject = "WORD-O-MAZE";
    string body = "GAME LINK HERE + DESCRIPTION";
    public void shareText()
    {
        //execute the below lines if being run on a Android device
#if UNITY_ANDROID
        //Refernece of AndroidJavaClass class for intent
        AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
        //Refernece of AndroidJavaObject class for intent
        AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
        //call setAction method of the Intent object created
        intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string[]>("EXTRA_EMAIL"), email);
        //set the type of sharing that is happening
        intentObject.Call<AndroidJavaObject>("setType", "text/plain");
        //add data to be passed to the other activity i.e., the data to be sent
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body);
        //get the current activity
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        //start the activity by sending the intent data
        currentActivity.Call("startActivity", intentObject);
#endif

    }

}

Hi! This code works fine, but when I add the lines for populate the recipient field (using EXTRA_EMAIL), It does´n work anymore. Can anyone help me?