Native Android share to a particular app in Unity

Suneet Agrawal
4 min readAug 13, 2018

--

In the previous medium posts “Native Android text sharing in Unity” and “Native Android screenshot/image sharing in Unity” we learnt about how to share some text or screenshot to other apps in a Unity app targeting Android platforms. In this post will target sharing the same text or screenshot to some particular app directly.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-text-sharing-to-particular-app-in-unity/ and reposted on Medium on 14th Aug 2018.

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

Direct accessibility is a feature that usually boosts the numbers as it reduces the number of clicks in an app. Sharing your score directly on some app can be one of them. Let’s take an example of WhatsApp in our case. There can be a use case where we want to provide a feature in the Unity app where a user can share their score in the form of text or image directly to WhatsApp.

Android provides the functionality where while sharing the text or image, we can provide the package name of the app to which we want to share directly. But before performing direct sharing, we need to confirm that the particular app is installed in the user’s device.

This can be done by getting a list of all the packages installed on users device and checking if the package is we are looking for is available in that list.

The Jave code for this check would be

//Java codepublic boolean checkIfAppInstalled(Context context, String packageName) {
List<PackageInfo> list = context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

for (PackageInfo item : list){
if (item.packageName.equalsIgnoreCase(packageName)){
return true;
}
}

return false;
}

And the Kotlin code would be

fun checkIfAppInstalled(context: Context, packageName: String): Boolean {

val list = context.packageManager
.getInstalledPackages(PackageManager.GET_ACTIVITIES)

for (item in list) {
if (item.packageName.equals(packageName, ignoreCase = true){
return true
}
}

return false
}

But where can I get the package name of the app I want to share on?

You can visit the Play Store page of that particular app and get the package name from the URL of that page.
It will be the marked as id=<packagename>

In the case of WhatsApp, it will be com.whatsapp

Now let’s try to achieve the same functionality of checking whether the sharing app is installed on the user’s device.

The C# code in Unity for the same will be

//C# codeprivate bool CheckIfAppInstalled(){

#if UNITY_ANDROID

string packageName = "com.whatsapp";

//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");

//get package manager reference
AndroidJavaObject packageManager = context.Call<AndroidJavaObject> ("getPackageManager");

//get the list of all the apps installed on the device
AndroidJavaObject appsList = packageManager.Call<AndroidJavaObject>("getInstalledPackages", 1);

//get the size of the list for app installed apps
int size = appsList.Call<int> ("size");

for (int i = 0; i < size; i++) {
AndroidJavaObject appInfo = appsList.Call<AndroidJavaObject> ("get", i);
string packageNew = appInfo.Get<string> ("packageName");

if (packageNew.CompareTo (packageName) == 0) {
return true;
}
}

return false;

#endif
}

Now that we got a check if the app is installed on the user’s device or not, we can share to the app (WhatsApp in our case) directly by simply setting the package to the intent. Rest all will still be the same as it was in the case of text sharing or screenshot sharing in Unity.

The java code for the same is

//JaveIntent intent = new Intent();
intent.setPackage(packageName);

and the Kotlin code would be

//Kotlinval intent = Intent()
intent.`package` = packageName

And the equivalent code in C# for Unity would be

//C# for Unitystring packageName = "com.whatsapp";

AndroidJavaObject intentObject =
new AndroidJavaObject ("android.content.Intent");

intentObject.Call ("setPackage", packageName);

And this will open the WhatsApp directly for sharing.

The entire script for Android native sharing to particular app in unity (C#) would be

The above script can be used in any unity app targeting Android platform to share a text or high score natively to any particular app in Android.

Update

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.

--

--