Android:发送短信时的 Unicode/字符集问题 (sendTextMessage) [英] Android: Unicode/Charset problems when sending an SMS (sendTextMessage)

查看:20
本文介绍了Android:发送短信时的 Unicode/字符集问题 (sendTextMessage)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个可以在收到短信后发送短信的工作应用程序.

Basically I have a working application that sends an SMS after receiving an SMS.

一切正常,除非要发送的短信具有特殊字符",即é,à,í,ç"等.

Everything works fine, except when the SMS text to send has "special chars", ie "é,à,í,ç", etc.

我尝试了很多事情,包括字符集转换,但我根本无法让它工作...... msgText 总是带着字符集编码问题回来.

I've tried many things including charset conversion but I simply can't make it work... the msgText always comes back with charset encoding problems.

这是发送消息的部分:

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 上应用的字符集转换函数(但没有帮助):

Here's the charset conversion function I tried (but didn't help), that I applied on 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 消息传递的解决方案(也没有帮助):

Near desperation I even tried the solution for unicode messaging in here (didn't help as well):

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

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

Anyway, here's the (cleaned up - package is com.THE.APPLICATION, main activity is MAINACT) LogCat for when it crashes (when trying to send the message, after receiving one):

WARN/dalvikvm(28218): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0)
ERROR/AndroidRuntime(28218): FATAL EXCEPTION: main
ERROR/AndroidRuntime(28218): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } in com.THE.APPLICATION.SMSReceiver@44acd880
ERROR/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.NullPointerException
ERROR/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

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

Sample of message text to send with issues:

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

所以,它读取 possÃvel它应该 possível

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

Please some enlightened soul help me out. He/She'll have a special place in my heart! :)

如果我心中的特殊位置没有削减它,我愿意为一个有效的解决方案支付几美元......

If the special place in my heart doesn't cut it, I'm willing to pay a few bucks for a working solution...

推荐答案

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

Ok, this seems to have been solved by simply using sendMultipartTextMessage instead of sendTextMessage for the messages.

谁能想到……这有点道理,因为 unicode 字符比普通"字符使用更多的空间".

Who would've thought... it kind of makes sense because unicode characters use more "space" than "normal" ones.

这篇关于Android:发送短信时的 Unicode/字符集问题 (sendTextMessage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆