Fondamentalement, j'ai une application de travail qui envoie un SMS après la réception d'un SMS.Android: problèmes Unicode/Charset lors de l'envoi d'un SMS (sendTextMessage)
Tout fonctionne bien, sauf si le texte SMS envoyer a "caractères spéciaux", soit "é, à, í, C", etc.
J'ai essayé beaucoup de choses, y compris charset conversion mais je ne peux tout simplement pas le faire fonctionner ... le msgText revient toujours avec des problèmes de codage charset.
est ici la partie où le message est envoyé:
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();
}
}
est ici la fonction de conversion de charset j'ai essayé (mais n'a pas aidé), que j'ai appliqué sur texteMsg:
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;
}
Près de désespoir J'ai même essayé la solution pour la messagerie unicode ici (n'a pas aidé aussi bien):
http://since2006.com/blog/android-send-unicode-message/
Quoi qu'il en soit, voici le (nettoyé - paquet est com.THE.APPLICATION, l'activité principale est MAINACT) LogCat quand il se bloque (en essayant d'envoyer le message, après avoir reçu un):
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 [email protected] 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
échantillon du texte du message à envoyer des questions:
VERBOSE/debug_tag(28218): msgText is: possÃvel.
Ainsi, il litpossÃvel quand il devrait être possivel
S'il vous plaît, une âme éclairée m'aide. Il/Elle aura une place spéciale dans mon coeur! :)
Edit: Si la place spéciale dans mon cœur ne coupe pas, je suis prêt à payer un peu d'argent pour une solution de travail ...
Ça a du sens, non? Mais cela ne fonctionne pas sans sendMultipartTextMessage avec des caractères de 8 bits (cela fonctionne évidemment pour les caractères de 7 bits sans cela). Je sens que quelque chose ne va pas vraiment bien là-dedans. –