0

I want to set phone number when I send message by hangout.

When I use sms, it can be done as below.

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", message);
sendIntent.putExtra("adress", phoneNumber);
context.startActivity(sendIntent);

But I don't know how set phone number or target by phone number in hangout.. Here is my current code using hangout.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
    {
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, message);

        if (defaultSmsPackageName != null)
        {
            sendIntent.setPackage(defaultSmsPackageName);
        }
        context.startActivity(sendIntent);
    }

EDITED..!

I found solution from here. See the @Roberto B.'s solution.

Community
  • 1
  • 1
Lee Jeongmin
  • 793
  • 11
  • 22

1 Answers1

1

I used the following code recently, and it seems to work as your require:

public static void sendSMS(Activity activity, String message, String phoneNumber){

        Intent smsIntent;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            smsIntent = new Intent(Intent.ACTION_SENDTO);
            //Ensures only SMS apps respond
            smsIntent.setData(Uri.parse("smsto:" + phoneNumber));

            //No resolvable activity
            if (smsIntent.resolveActivity(activity.getPackageManager()) == null) {
                return;
            }

        }else{
            //Old way of accessing sms activity
            smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", phoneNumber);
            smsIntent.putExtra("exit_on_sent", true);
        }
        smsIntent.putExtra("sms_body", message);
        activity.startActivity(smsIntent);
    }
Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
  • Thanks for your comment!! I tested your code. But in kitkat, smsIntent.resolveActivity(activity.getPackageManager()) returns null. – Lee Jeongmin May 15 '15 at 02:31