It took me a while to figure this out. All solutions found on the Internet were only partially completed. Below is the complete solution to launch a Unity application from another Android application using a custom implicit intent, as well as access to additional data sent with intent inside Unity.
To do this, you need to create an Android plugin that will be used by Unity to access additional intent data.
ANDROID PLUGIN:
You need to copy class.jar from the Unity installation folder to the plugin folder android / classes.jar .//
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); } }
AndroidManifest.xml
It is important to use the package name: com.company.plugin
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Gradle build file:
Add the following to your gradle build file: to create a .jar file for use with Unity
android { compileSdkVersion 23 buildToolsVersion "23.0.2" sourceSets { main { java { srcDir 'src/main/java' } } } ... ... dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.android.support:design:23.2.1' compile files('libs/classes.jar') } //task to delete the old jar task deleteOldJar(type: Delete) { delete 'release/AndroidPlugin.jar' } //task to export contents as jar task exportJar(type: Copy) { from('build/intermediates/bundles/release/') into('release/') include('classes.jar') ///Rename the jar rename('classes.jar', 'AndroidPlugin.jar') } exportJar.dependsOn(deleteOldJar, build)
Copy the generated AndroidPlugin.jar to Unity Assets / Plugins / Android
UNITY APP:
Set the package identifier in PlayerSettings to the same settings as in Android Plugin - com.company.plugin
Create your own AndroidManifest.xml file in Assets / Plugins / Android
It is important here to use the same package name that is used in the plugin. Also note the name of the intent: com.company.plugin.do
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:launchMode="singleTask" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="sensor"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="com.company.plugin.do" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain"/> </intent-filter> </activity> </application> </manifest>
Create a unity script called AccessManager and attach the script to the game object in the scene. OnAccessToken is a method that will receive a message sent from the android plugin and will contain additional data sent with intent.
public class accessManager : MonoBehaviour { public void OnAccessToken(string accessToken) { Debug.Log("Message Received!!!! :" + accessToken); } }
ANDROID APP:
Create a standard Android app that launches the Unity app and sends additional intent information
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); }