收到正确编码的读取短信,并转换为UTF8 [英] Receive an read SMS with correct encoding and convert to UTF8
问题描述
我写一个BroadcastReceiver接收短信。我将需要该消息与由在设置用户设置的字符串进行比较,但字符串可以是非ASCII。下面code仅适用于那些只包含ASCII字符的短信-ES。我怎么能转换消息UTF8编码?
I am writing a BroadcastReceiver to receive SMS messages. I will need to compare the message with a string that was set by the user in the settings, but the string can be non-ASCII. The below code works only for SMS-es that contain only ASCII characters. How can I convert the message to UTF8 encoding?
public class SmsReceiver extends BroadcastReceiver {
final static String TAG = "SmsReceiver";
public SmsReceiver() {
}
@Override
public void onReceive(final Context context, final Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
final SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
final String senderNum = currentMessage.getDisplayOriginatingAddress();
final String message = currentMessage.getDisplayMessageBody();
Log.i(TAG, "senderNum: " + senderNum + "; message: " + message);
更新:
似乎在同一code工作对我Nexus5,但在仿真器API 10不起作用。
it seems that the same code works on my Nexus5, but doesn't work in the emulator with API 10.
我发送的文本:שלום
我收到的文本:éRΨ¿ÑU $
推荐答案
有关记录,如果别人进入类似的问题,这似乎是在模拟器中的错误。我打开Android模拟器一票:
的https://$c$c.google.com/p /安卓/问题/细节?ID = 202723
For the record, if someone else gets into similar problems, it seems to be a bug in the emulators. I opened a ticket for android emulator: https://code.google.com/p/android/issues/detail?id=202723
这篇关于收到正确编码的读取短信,并转换为UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!