Native Android share to a particular app in Unity

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.
//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;
}
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
}
//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
}
//JaveIntent intent = new Intent();
intent.setPackage(packageName);
//Kotlinval intent = Intent()
intent.`package` = packageName
//C# for Unitystring packageName = "com.whatsapp";

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

intentObject.Call ("setPackage", packageName);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store