Android 发送长短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:03:48  来源:igfitidea点击:

Sending long sms messages

android

提问by Mark

I've got an app that lets users send sms messages. Works great when the message < 160 characters. After that, things work less-perfectly. Seems like there are a few options here:

我有一个应用程序可以让用户发送短信。当消息 < 160 个字符时效果很好。在那之后,事情就不那么完美了。似乎这里有几个选项:

  1. Manually break the message up into multiple SMSs, send each part as a separate SMS.
  2. Use the multi-part send SMS function (sendMultipartTextMessage()).
  3. Send the message as an MMS message (sendDataMessage()?).
  1. 手动将消息拆分为多个 SMS,将每个部分作为单独的 SMS 发送。
  2. 使用多部分发送短信功能 (sendMultipartTextMessage())。
  3. 将消息作为彩信发送 (sendDataMessage()?)。

Here's my novice take on it:

这是我的新手对它的看法:

1) most well supported across carriers. Users may get mad that you just cost them N separate messages though, instead of converting to MMS or something.

1) 最受运营商支持。用户可能会因为你只是花费他们 N 条单独的消息而不是转换为彩信或其他东西而生气。

2) not sure if this is supported by different carriers, and read that once the message is greater than 3 * 160 chars in length, gets converted to MMS anyway by different SMS apps - maybe stay away from this altogether.

2) 不确定这是否受不同运营商的支持,并且一旦消息长度超过 3 * 160 个字符,就会被不同的 SMS 应用程序转换为彩信 - 可能完全远离这种情况。

3) not sure how to do this, and older phones might not support MMS. To send an MMS using the android SDK, do we just use the SmsManager.sendDataMessage() method?

3) 不知道怎么做,旧手机可能不支持彩信。要使用 android SDK 发送彩信,我们是否只使用 SmsManager.sendDataMessage() 方法?

Thanks

谢谢

采纳答案by sap

seems to me that the first option is what most mobile phones do by default. sms messages by design can only send a certain amount of characters (160 probbaly), just inform the user that the message is too big and if he still wants to send it anyway (informing also how many sms would the total be).

在我看来,第一个选项是大多数手机默认所做的。短信设计只能发送一定数量的字符(160 probbaly),只需通知用户消息太大,如果他仍然想发送它(还通知总共有多少条短信)。

as for MMS and multipart as you said not every carrier supports it, so they dont seem to be the best option.

至于 MMS 和 multipart,正如您所说,并非每个运营商都支持它,因此它们似乎不是最佳选择。

EDIT: as for how does MMS work on android-sdk check this thread out: Android SDK MMS

编辑:至于 MMS 如何在 android-sdk 上工作,请查看此线程:Android SDK MMS

回答by Valentin Klinghammer

This is quite an old post but it's high up on Google when searching for Android multipart sms, so maybe it helps someone.

这是一篇很老的帖子,但在搜索 Android 多部分短信时,它在 Google 上名列前茅,所以也许它对某人有所帮助。

Regarding part 1 and 2, it's pretty much the same thing. To use sendMultipartTextMessage, you need to break up the long message into an ArrayList of Strings. It then sends as many SMS as needed. In short:

关于第 1 部分和第 2 部分,它几乎是相同的。要使用sendMultipartTextMessage,您需要将长消息分解为字符串的ArrayList。然后它会根据需要发送尽可能多的短信。简而言之:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(longMessage);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

Part 3: MMS is not an option, as it has been pointed out. The charges and all.

第 3 部分:正如已经指出的那样,MMS 不是一种选择。费用和所有。

回答by Mohit Sethi

I would suggest the usage of option 2 when you are working on Androids GSM based handsets.

当您使用基于 Android GSM 的手机时,我建议您使用选项 2。

GSM based Mobile device takes care of segmentation in which breaking the messages to multiparts for sending in performed and also assembling of the multipart messages in to one message on receipt.

基于 GSM 的移动设备负责分段,其中将消息分成多部分进行发送,并将多部分消息在收到时组合成一个消息。

If you have a method which takes care of sending text messages, than by default use the options of manager.divideMessage as it will work even if the message segments required are only 1 in number.

如果你有一个方法来处理发送文本消息,那么默认情况下使用 manager.divideMessage 的选项,因为即使所需的消息段只有 1 个,它也能工作。

I dont think you should have any problem sending messages using option 2 and it will also ensure that the receiver receives the message as a single message. Else we need to write your our own protocol stack where in you write reference number and number of messages for the receipient to understand and recreate the complete message; which is not very tough. We can use byte arrays with headers and the messages can be sent as base64 encoded.

我认为您使用选项 2 发送消息不会有任何问题,它还将确保接收方将消息作为单个消息接收。否则,我们需要编写您自己的协议栈,在其中编写参考编号和消息数量,以便接收方理解和重新创建完整的消息;这不是很艰难。我们可以使用带有标头的字节数组,并且消息可以作为 base64 编码发送。

Also I dont know much about the limits the carriers enforce on the number of segments in multipart message; based on my test I was able to send and receive 160*8 segments properly. Based on the GSM standards the segments can be upto 255 but that count might depend on the service provider implementation.

此外,我不太了解运营商对多部分消息中的段数实施的限制;根据我的测试,我能够正确发送和接收 160*8 段。根据 GSM 标准,分段最多可达 255 个,但该数量可能取决于服务提供商的实施。