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