TTS 读出收到的短信,如果短信超过正常 160 个字符 - 将停止
如标题所示。使用 TTS 朗读收到的消息。但是,如果消息超过 160 个字符,它将不会读取其余部分(我认为从技术上讲,这是第二条文本,由网络提供商链接到一条“大”消息)我如何修改我的代码,以便如果短信大于标准的单条消息,我可以全部读出来吗? 这是我的代码片段
public void onReceive(Context context, Intent intent)
{
int n;
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (n = 0; n<messages.length; n++){
smsMessage[n] = SmsMessage.createFromPdu((byte[])messages[n]);
}
String sms1 = smsMessage[0].getMessageBody();
/**Send variable to the class handling TTS, to be read out-loud by the corresponding method */
SpeakerActivity.speakSMS(sms1);
as in title. Have TTS reading out received messages. But if the message exceeds 160 characters, it will no read the rest (which I assume is technically a second text, linked into one "big" message by the network provider) How can I modify my code so that if the sms is bigger than the standard single message, I can read it all out?
Here is a snippet of my code
public void onReceive(Context context, Intent intent)
{
int n;
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (n = 0; n<messages.length; n++){
smsMessage[n] = SmsMessage.createFromPdu((byte[])messages[n]);
}
String sms1 = smsMessage[0].getMessageBody();
/**Send variable to the class handling TTS, to be read out-loud by the corresponding method */
SpeakerActivity.speakSMS(sms1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例中数组的每个成员都包含消息的 160 个字符部分。如果消息恰好超过 160 个字符,尽管 Android 在默认 SMS 应用程序中将它们拼接在一起,但它们将在数组中以部分形式表示。您需要迭代该数组以查找每条长消息的附加部分。
Each member of the array in your example contains a 160 character part of a message. If the message happens to exceed 160 characters, though Android stitches these together in the default SMS app, they will be represented in parts in your array. You'll need to iterate over that array to find the additional pieces of each long message.
smsMessage[1].getMessageBody();
smsMessage[2].getMessageBody();
等包含短信的“其余部分”。解决了。
smsMessage[1].getMessageBody();
smsMessage[2].getMessageBody();
etc contain the "rest" of the sms. Solved.