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

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Design VUI for Autonomous Vehicle Process Document

.Net Assemblies

How to augment in real world using QRCode

What the new Scrum Guide 2020 means for our daily job!

2D Shooter: Adding an Aggressive Enemy

Setting Up A Docker Registry With HTTPS(LetsEncrypt) and Basic Authentication(htpasswd)

Bootstrapping a SaaS sideproject with 0$ and 0 experience: a story.

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
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Implementing Android 6.0 permissions in Unity3D

Unity native GPS plugin (iOS and Android)

How To Inspect Elements On Android Devices?

Dependency Injection With Hilt