Native Android text sharing in Unity app

In continuation of my previous medium post “Native Android in Unity”, will consider another example of native Android text sharing functionality in the unity app. This is a very common requirement in any unity app targeting Android platform to share the high score or challenge other players with a text message or a screenshot of the high score. The sharing can be done via any of the app available on the user’s device which supports sharing.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-text-sharing-in-unity/ and reposted on Medium on 29th Jun 2018.

Android provides a native share functionality to do the same where we provide the text and/or the image to share to the Android system and Android provides a list of available apps on the device to share, where the user can pick one app to perform the share action.

Now let’s see first how sharing works in native Android.

Android uses Intent to do the same.
For those who are new to native Android, An intent is an abstract description of an operation to be performed.
In an intent, we can set the action to be performed (sharing in our case), set the type of extras we will share ( text, image or any other format), pass all the extras to be shared (text and/or image) and finally call the Android system to provide the list of the available apps in the device which accepts this type of sharing.

The java code for the same will be

//Java
String shareSubject = "I challenge you to beat my high score in Fire Block";
String shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock";

And the Kotlin code will be

//Kotlon
val shareSubject = "I challenge you to beat my high score in Fire Block"
val shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock"

Let’s see how we can use the same native Android share functionality in a unity app. As in the previous post about “Native Android in Unity” we learnt that we can use all the native android classes in unity using AndroidJavaClass and AndroidJavaObject.

Please continue reading at https://agrawalsuneet.github.io/blogs/native-android-text-sharing-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.