6

I am creating an app where user clicks on different social media links. The links are from different media including Facebook, Youtube and WhatsApp etc.

What I want is:

When user clicks on a Facebook link, the program checks if there is Facebook app installed in the user's phone and open the link in it. Similarly for other media.

I have tried a little with this code:

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        context.startActivity(intent);

But it opens all the link in browser. I am new to android, kindly help! Thanks in advance...

Muzzammil Hussain
  • 161
  • 1
  • 1
  • 7

1 Answers1

1

The package name can be used to launch the application.

Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name");
if (intent != null) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} else {
    // Open GooglePlay link to install the app
}
Aspicas
  • 4,498
  • 4
  • 30
  • 53
  • Thanks for your response. The package name works for WhatsApp only, not for links of other media (facebook,etc.).For WhatsApp the package name is "com.whatsapp". For other links the intent remains null. – Muzzammil Hussain Nov 12 '19 at 10:54