Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Created March 6, 2021 05:50
Show Gist options
  • Select an option

  • Save shibbirweb/46fc77d92f3f2ccc55bb8b3dee84591e to your computer and use it in GitHub Desktop.

Select an option

Save shibbirweb/46fc77d92f3f2ccc55bb8b3dee84591e to your computer and use it in GitHub Desktop.
Android: Open social profile on native app
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
object SocialLinkHelper {
/**
* Handle Facebook social link click
*/
fun facebook(context: Context, username: String) {
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=https://www.facebook.com/$username"))
context.startActivity(intent)
} catch (e: Exception) {
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.facebook.com/$username")
)
context.startActivity(intent)
}
}
/**
* Handle LinkedIn social link click
*/
fun linkedIn(context: Context, username: String) {
var intent = Intent(Intent.ACTION_VIEW, Uri.parse("linkedin://add/%@$username"))
val packageManager: PackageManager = context.packageManager
val list = packageManager.queryIntentActivities(intent!!, PackageManager.MATCH_DEFAULT_ONLY)
if (list.isEmpty()) {
intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("http://www.linkedin.com/profile/view?id=$username")
)
}
context.startActivity(intent)
}
/**
* Handle Whatsapp social link click
*/
fun whatsapp(context: Context, phoneNumber: String) {
val url = "https://api.whatsapp.com/send?phone=$phoneNumber"
try {
val pm = context.packageManager
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES)
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url)
context.startActivity(intent)
} catch (e: PackageManager.NameNotFoundException) {
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
)
context.startActivity(intent)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment