How to open Whatsapp through a Xamarin.Android (not Xamarin.forms) app?

Viewed 515

I've been researching a way to open whatsapp through a xamarin app, but all i found were recommendations using xamarin.forms and not xamarin.android (which the app I work on is based on). Is there a way to do this in xamarin.android?

1 Answers

Well, a basic code like that should do it I guess:

 private void OpenWhatsApp(Activity activity, string number, string message)
    {

        try
        {
            PackageManager packageManager = activity.PackageManager;
            Intent i = new Intent(Intent.ActionView);
            String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.Encode(message, "UTF-8");
            i.SetPackage("com.whatsapp");
            i.SetData(Uri.Parse(url));
            if (i.ResolveActivity(packageManager) != null)
            {
                activity.StartActivity(i);
            }
            else
            {
                //Error message
            }
        }
        catch (Exception e)
        {
            // exception
        }

    }

Let me know if you have any queries!

Related