当用户点击某个按钮时,默认的短信应用程序应该打开,而且我也在向我发送该号码的消息,但是我现在正在使用的代码是下面的代码。
private void sendSMS(String _Mobile_Number) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context); // Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "text to send");
// sendIntent.setData(Uri.parse("sms:" + _Mobile_Number));
if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
context.startActivity(sendIntent);
} else // For early versions, do what worked for you before.
{
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", _Mobile_Number);
smsIntent.putExtra("sms_body", "");
context.startActivity(smsIntent);
}
}
当我使用下面的行时,它给我例外。
sendIntent.setData(Uri.parse("sms:" + _Mobile_Number));
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=sms:xxxxxxxxxxxx flg=0x1 pkg=com.google.android.talk (has clip) (has extras) }
如果我不使用该行,那么我的默认应用程序会打开,但它会显示联系人列表。当我选择一个联系人时,我会看到我写的“要发送的文字”的文字。但我不想要这个
我想要的是当我点击我的默认应用程序打开按钮时,在联系人文本框中我提供的号码应该在那里。
答案 0 :(得分:0)
试试这个版本
Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(Intent.createChooser(intent, "Send sms via:"));
答案 1 :(得分:0)
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
Uri _uri = Uri.parse("tel:" + _Mobile_Number);
Intent sendIntent = new Intent(Intent.ACTION_VIEW, _uri);
sendIntent.putExtra("address",_Mobile_Number);
sendIntent.putExtra("sms_body", "text to send");
sendIntent.setPackage(defaultSmsPackageName);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);