gpt4 book ai didi

Android:发送短信 (sendTextMessage) 时出现 Unicode/Charset 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:02 34 4
gpt4 key购买 nike

基本上,我有一个可以在收到 SMS 后发送 SMS 的工作应用程序。

一切正常,除非 SMS 文本发送有“特殊字符”,即“é、à、í、ç”等。

我已经尝试了很多东西,包括字符集转换,但我就是无法让它工作...msgText 总是返回字符集编码问题。

这是发送消息的部分:

if (msgText.length() > 160) {
ArrayList msgTexts = sm.divideMessage(msgText);
sm.sendMultipartTextMessage(PhoneNumber, null, msgTexts, null, null);
} else {
try {
sm.sendTextMessage(PhoneNumber, null, msgText, null, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}

这是我在 msgText 上应用的字符集转换函数(但没有帮助):

public static String formatCharset(String txtInicial) {
//-- Please notice this is just for reference, I tried every charset from/to conversion possibility. Even stupid ones and nothing helped.

/*try {//-- Seems simpler, it should do the same as below, but didn't help
msgText = new String(msgText.getBytes("UTF-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/

Charset charsetOrigem = Charset.forName("UTF-8");
CharsetEncoder encoderOrigem = charsetOrigem.newEncoder();
Charset charsetDestino = Charset.forName("ISO-8859-1");
CharsetDecoder decoderDestino = charsetDestino.newDecoder();

String txtFinal = "";

try {
ByteBuffer bbuf = encoderOrigem.encode(CharBuffer.wrap( txtInicial ));
CharBuffer cbuf = decoderDestino.decode(bbuf);
txtFinal = cbuf.toString();
} catch (CharacterCodingException e) {
e.printStackTrace();
}

if (txtFinal.length() == 0) txtFinal = txtInicial;

return txtFinal;
}

几近绝望,我什至在这里尝试了 unicode 消息传递的解决方案(也没有帮助):

http://since2006.com/blog/android-send-unicode-message/

无论如何,这里是(清理后 - 包是 com.THE.APPLICATION,主要 Activity 是 MAINACT)当它崩溃时的 LogCat(当尝试发送消息时,收到消息后):

WARN/dalvikvm(28218): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0)ERROR/AndroidRuntime(28218): FATAL EXCEPTION: mainERROR/AndroidRuntime(28218): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } in com.THE.APPLICATION.SMSReceiver@44acd880ERROR/AndroidRuntime(28218):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)ERROR/AndroidRuntime(28218):     at android.os.Handler.handleCallback(Handler.java:587)ERROR/AndroidRuntime(28218):     at android.os.Handler.dispatchMessage(Handler.java:92)ERROR/AndroidRuntime(28218):     at android.os.Looper.loop(Looper.java:123)ERROR/AndroidRuntime(28218):     at android.app.ActivityThread.main(ActivityThread.java:4627)ERROR/AndroidRuntime(28218):     at java.lang.reflect.Method.invokeNative(Native Method)ERROR/AndroidRuntime(28218):     at java.lang.reflect.Method.invoke(Method.java:521)ERROR/AndroidRuntime(28218):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)ERROR/AndroidRuntime(28218):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)ERROR/AndroidRuntime(28218):     at dalvik.system.NativeStart.main(Native Method)ERROR/AndroidRuntime(28218): Caused by: java.lang.NullPointerExceptionERROR/AndroidRuntime(28218):     at android.os.Parcel.readException(Parcel.java:1253)ERROR/AndroidRuntime(28218):     at android.os.Parcel.readException(Parcel.java:1235)ERROR/AndroidRuntime(28218):     at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)ERROR/AndroidRuntime(28218):     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)ERROR/AndroidRuntime(28218):     at com.THE.APPLICATION.MAINACT.sendMessage(MAINACT.java:214)ERROR/AndroidRuntime(28218):     at com.THE.APPLICATION.SMSReceiver.onReceive(SMSReceiver.java:24)ERROR/AndroidRuntime(28218):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)ERROR/AndroidRuntime(28218):     ... 9 more

发送有问题的消息文本示例:

VERBOSE/debug_tag(28218): msgText is: possível.

因此,它读取possível应该possível

请一些开明的灵魂帮助我。他/她将在我心中占有特殊的位置! :)

编辑:如果我心中的特殊位置不适合,我愿意花几 block 钱买一个可行的解决方案...

最佳答案

好的,这似乎已经通过简单地使用 sendMultipartTextMessage 而不是 sendTextMessage 来解决消息。

谁会想到...这是有道理的,因为 unicode 字符比“普通”字符使用更多的“空格”。

关于Android:发送短信 (sendTextMessage) 时出现 Unicode/Charset 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233461/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com