How to send message from Android app through Viber message

12,264

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"));
Share:
12,264
vonbk
Author by

vonbk

Updated on June 24, 2022

Comments

  • vonbk
    vonbk about 2 years

    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

  • vonbk
    vonbk over 10 years
    Hi Rethinavel Velu, How can I specify the outgoing phone number. I added : share.putExtra(Intent.EXTRA_PHONE_NUMBER,"xxxxxx"); But it does not work well.
  • Rethinavel
    Rethinavel over 10 years
    What do you mean by specifying outgoing number..? You want to share the phone number to viber or what?
  • vonbk
    vonbk over 10 years
    I meant that I want to send message ""Your text to share" to my friend whom has phone number is "xxxxxxx".
  • Rethinavel
    Rethinavel over 10 years
    Viber will ask you to do so, I believe. I think you will send directly. I am not sure about that. Let me check and revert you back
  • vonbk
    vonbk over 10 years
    Yes, Viber asked me to do so as you said. But it is not my expectation. I want to Viber send message to "xxxx" number without asking me to select phone number. On other hand, I want Viber has same function as following SMS code: smsManager.sendMultipartTextMessage("xxxxxx", null, messageArray, null,null);
  • Rethinavel
    Rethinavel over 10 years
    Oh i see. I don't know how to implicitly send a number.
  • vonbk
    vonbk over 10 years
    Thank you so much for your effort constantly.
  • mahdi
    mahdi almost 10 years
    @vonbk did you find any way to send number to viber ?