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

后端 未结 5 2076
梦毁少年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: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 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.

提交回复
热议问题