Android: Unicode/Charset problems when sending an SMS (sendTextMessage)

后端 未结 5 2050
梦毁少年i
梦毁少年i 2020-11-30 07:10

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

Everything works fine, except when the SMS text to send

相关标签:
5条回答
  • 2020-11-30 07:29

    You can only use 160 chars if it's 7-bit encoding. If you use 140 chars, it should work without sendMultipartTextMessage because you're using 8-bit chars (UTF-8).

    0 讨论(0)
  • 2020-11-30 07:37

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

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

    0 讨论(0)
  • 2020-11-30 07:38

    I am using this command line:

    Html.fromHtml(new String(myString.getBytes("UTF-8"))).toString();
    

    and my SMS message looks perfect.

    0 讨论(0)
  • 2020-11-30 07:39

    I has the same problem with special characters. When I change message MAX_SMS_MESSAGE_LENGTH to 70 everything works well. Look at that link:

    https://forums.macrumors.com/threads/special-characters-in-sms-turn-them-shorter-70-characters-old-issue-never-solved.1030577/

    This is my code:

        public static void sendSMS(String phoneNumber, String message, Context context) {
    
        String SENT = "SMS_SENT";
        int MAX_SMS_MESSAGE_LENGTH = 70;
    
        SmsManager smsManager = SmsManager.getDefault();
        PendingIntent sentPI;
        sentPI = PendingIntent.getBroadcast(context, 0,new Intent(SENT), 0);
    
        try {
            if(message.length() > MAX_SMS_MESSAGE_LENGTH) {
                ArrayList<String> messageList = SmsManager.getDefault().divideMessage(message);
                smsManager.sendMultipartTextMessage(phoneNumber, null, messageList, null, null);
            } else {
                smsManager.sendTextMessage(phoneNumber, null, message, sentPI, null);
            }
        } catch (Exception e) {
            Log.e("SmsProvider", "" + e);
        }
    }
    

    Of course you can insert a controller that will check if the message contains some special character and then change MAX_SMS_MESSAGE_LENGTH from 160 to 70. I always have special characters in my app.

    0 讨论(0)
  • 2020-11-30 07:47

    I have used this code to convert UTF-8 characters into ASCII. Then sending of SMS works and I can use 160 characters:

    private static final String PLAIN_ASCII = "AaEeIiOoUu" // grave
            + "AaEeIiOoUuYy" // acute
            + "AaEeIiOoUuYy" // circumflex
            + "AaOoNn" // tilde
            + "AaEeIiOoUuYy" // umlaut
            + "Aa" // ring
            + "Cc" // cedilla
            + "OoUu" // double acute
    ;
    
    private static final String UNICODE = "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
            + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
            + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
            + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1"
            + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF"
            + "\u00C5\u00E5" + "\u00C7\u00E7" + "\u0150\u0151\u0170\u0171";
    
    // remove accentued from a string and replace with ascii equivalent
    public static String convertNonAscii(String s) {
        if (s == null)
            return null;
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int pos = UNICODE.indexOf(c);
            if (pos > -1) {
                sb.append(PLAIN_ASCII.charAt(pos));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    
    0 讨论(0)
提交回复
热议问题