How to send message from Android app through Viber message

vonbk picture vonbk · Oct 30, 2013 · Viewed 12k times · Source

I want to write small Android app to send the message through Viber to people whom are listed in my contact list. But I could not find any sample code to do this task. If you know how to do this task.

Please teach me.

Vonbk

Answer

Rethinavel picture Rethinavel · Oct 30, 2013

If viber application is installed in your device, You can call an intent to share the text.

boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");

        // gets the list of intents that can be loaded.
        List<ResolveInfo> resInfo = context.getPackageManager()
                .queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                if (info.activityInfo.packageName.toLowerCase(
                        Locale.getDefault()).contains("com.viber.voip")
                        || info.activityInfo.name.toLowerCase(
                                Locale.getDefault()).contains("com.viber.voip")) {
                    share.putExtra(Intent.EXTRA_TEXT, "Your text to share");
                    share.setPackage(info.activityInfo.packageName);
                    found = true;
                    context.startActivity(Intent.createChooser(share, "Select"));
                    break;
                }
            }
            if (!found) {

                displayToast(context, "Install viber android application");
                Uri marketUri = Uri.parse("market://details?id="
                        + "com.viber.voip");
                Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
                context.startActivity(marketIntent);
            }

        }

I am not sure it will work. But it will worth a shot.

You can also share with the plain intent which asks the user to select and share :

Like this

         Intent sharingIntent = new Intent(Intent.ACTION_SEND);    
         sharingIntent.setType("text/html"); 
         sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>")); 
        startActivity(Intent.createChooser(sharingIntent,"Share using"));