Blog / June 26, 2021 / 4 mins read / By Suneet Agrawal

Native Android Receive Text from Other Apps in Unity

In continuation to my previous blog Native Android text sharing in Unity, where we learnt about sharing text from a Unity app targeting the Android platform, let’s try to learn about how can we receive a text from other apps. Accepting a text from other apps is not that common but sometimes location-based games required to accept text or location shared from other apps. There can be a generic use case also where we want to accept some text from other apps for any purpose.

If you have not read the previous blogs, I would strongly recommend to read them first. You can read them on the links below.

Android provides a native share functionality to handle text or multimedia sharing. Any app can trigger an intent along with extra params to share a text. To receive the text or show our app in the sharing intent list handled by Android, we majorly have to do two things.

  1. Register Intent Filter in Manifest file.
  2. Extract text from the Intent object shared by the other app.

Register Intent Filter in Manifest file

The Android manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play. Every Android app or unity app running on the Android platform must have a Manifest file. The manifest file contains information about package name, permissions, application name, icon, activities, services, providers and much more.

To add an intent filter to the manifest file in an activity tag, first, we need to add a custom manifest file. We can add a custom manifest file and Unity will generate a merged manifest file at the end which will be shipped along with the final apk generated.

We can copy a basic structure of the custom manifest file from /Temp/StagingArea/AndroidManifest.xml

And can paste the modified AndroidManifest.xml at Assests/Plugins/Android/AndroidManifest.xml

Please note that if this file doesn’t exist for you, change the target platform to Android and build the Unity project once. It will generate the Temp folder and this file inside it. This is a temporary file that is created by Unity but we can copy it.

The copied manifest file will be of xml format and will look like something below.

To enable accepting text from other apps, we need to add an Intent Filter to the activity tag. There can be multiple activity tags but there will at least be one activity tag with the name com.unity3d.player.UnityPlayerActivity.

That will also contain one intent-filter tag already with category android.intent.category.LAUNCHER

We need to add one more as below.

<intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
</intent-filter>

To understand what is intent filter and why we added this, we need to understand what is Intent first.

An Intent is a messaging object which is used to start an activity, a service or deliver a broadcast.

Intent is of two types

  • Explicit Intent : An explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app.
  • Implicit Intent : An implicit intent specifies an action that can invoke any app on the device able to perform the action.

Any app sharing the text will be triggering an implicit action. In other to catch that implicit intent triggered by other apps we need to define an intent filter in the manifest file as we did along with category, action and data tags.

The final manifest file will look like below.

Extract text from the Intent object shared by the other app

This is a straightforward part. We need to get the reference of the current activity, get the intent out of it and simply get the extra text out of it.

If you read the previous blog Native Android in Unity, you can easily understand how can we get the current activity reference and the context object.

//create a class reference of unity player activity
AndroidJavaClass unityActivity = 
        new AndroidJavaClass ("com.unity3d.player.UnityPlayer");

//get the context of current activity
AndroidJavaObject context = unityActivity.GetStatic<AndroidJavaObject> ("currentActivity");

To get the intent object, we just need to call getIntent on the content object. And later we can get the string extra using getStringExtra on the intent object which will return the string shared by other apps.

//get the current intent object
AndroidJavaObject intent = context.Call<AndroidJavaObject>("getIntent");

//get the extra text from intent
string incomingText = intent.Call<string>("getStringExtra", "android.intent.extra.TEXT");

The overall code will look like below.

We can attach the above script to any game object and extract the text shared by other apps.

Comments