Reputation: 893
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.
Upvotes: 0
Views: 892
Reputation: 5149
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);
}
Upvotes: 1