用于发送完美无缺的短信代码直到Android 4.3(Jelly Bean)自4.4(KitKat)起停止工作
我只是为用户准备了短信,但他需要选择要发送到
的号码我使用的代码是:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
由于它停止了工作,我还尝试了ACTION_SEND
和ACTION_SENDTO
两者都没有用,我也尝试了sendIntent.setType("vnd.android-dir/mms-sms");
,再没有任何效果。
我在stackoverflow answer 1和answer 2中寻找了几个答案,但这两个答案都没有处理我的要求。
我想做什么:
对于主持人: 这不是一个重复的问题,因为问题并没有提出完全相同的问题,这里需要的是发送没有电话号码的短信,并且没有任何问题和答案处理过。
答案 0 :(得分:78)
我通过执行以下操作附加了解决问题的代码:
以下是代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);
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);
}
activity.startActivity(sendIntent);
}
else //For early versions, do what worked for you before.
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
}
答案 1 :(得分:19)
这个适用于所有Android版本和所有短信应用(包括环聊)。
public static boolean sendSms(Context context, String text, String number) {
return sendSms(context, text, Collections.singletonList(number));
}
public static boolean sendSms(Context context, String text, List<String> numbers) {
String numbersStr = TextUtils.join(",", numbers);
Uri uri = Uri.parse("sms:" + numbersStr);
Intent intent = new Intent();
intent.setData(uri);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra("sms_body", text);
intent.putExtra("address", numbersStr);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_SENDTO);
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
if(defaultSmsPackageName != null) {
intent.setPackage(defaultSmsPackageName);
}
} else {
intent.setAction(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
}
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
答案 2 :(得分:17)
结合提议的解决方案,以下内容可以预设收件人和文本。
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact.toString())));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact.toString());
intent.putExtra("sms_body", text);
}
activity.startActivity(intent);
剩下的唯一问题是你最终会遇到环聊(Nexus 5),你可能需要多次按“后退”才能有效取消短信。
答案 3 :(得分:0)
在Kotlin中,以下代码有效:
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
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)
activity!!.startActivity(sendIntent)
}