Native Android Receive Text from Other Apps in Unity

Suneet Agrawal
3 min readDec 3, 2021

--

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 are 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.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-receive-text-from-other-apps-in-unity/ and reposted on Medium on 03rd Dec 2021.

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.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.agrawalsuneet.unitysharingclient" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal">  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />  <application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner">    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />      </intent-filter>      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />    </activity>    <meta-data android:name="unity.build-id" android:value="d23ed252-3184-42bb-8dac-d4a3f9f6b02a" />    <meta-data android:name="unity.splash-mode" android:value="0" />    <meta-data android:name="unity.splash-enable" android:value="True" />  </application>  <uses-feature android:glEsVersion="0x00020000" />  <uses-permission android:name="android.permission.INTERNET" />  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" /></manifest>

Please continue reading at https://agrawalsuneet.github.io/blogs/native-android-receive-text-from-other-apps-in-unity/

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--