Blog / August 14, 2020 / 4 mins read / By Suneet Agrawal

Native Android text sharing to particular app in Unity

In the previous blogs Native Android text sharing in Unity and Native Android 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 image to some particular app directly.

If you have not read the previous blogs, 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
public 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

//Kotlin
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>

Native Android text sharing to particular app in Unity

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# code
private 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
}

“Native Android text sharing to particular app in Unity

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

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

and the Kotlin code would be

//Kotlin
val intent = Intent()
intent.`package` = packageName

And the equivalent code in C# for Unity would be

//C# for Unity
string packageName = "com.whatsapp";
 
AndroidJavaObject intentObject = 
             new AndroidJavaObject ("android.content.Intent");
 
intentObject.Call ("setPackage", packageName);

And this will open the WhatsApp directly for sharing.

Native Android text sharing to particular app in Unity

The final script will look like below.

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:

Comments