Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /var/www/since2006.com/public_html/include/util/Ubb.php on line 249

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /var/www/since2006.com/public_html/include/util/Ubb.php on line 249

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /var/www/since2006.com/public_html/include/util/Ubb.php on line 249
android下发送中文短消息 - since.2006 -  

since.2006  

在android中用直接使用smsManager.sendTextMessage()方法发送中文或其它unicode字符会显示不正确。

目前来说想正确发送中文,需要自己构造PDU,然后在JAVA中用反射调用SmsManager的private sendRawPdu方法来实现。

参考:http://www.android123.com.cn/androidkaifa/180.html

上文中只说了PDU构造实现原理,具体怎么使用没有涉及。俺这再补一把,直接贴出代码。其实android本身SDK就有相应方法以UCS2编码发送,只是默认不会调用。

以下代码G1下测试通过

public class SmsHelper {

 public static final int MAX_USER_DATA_BYTES = 140;

 private Context mContext;

 public SmsHelper(Context c) {
  mContext = c;
 }

 public void sendUnicodeMessage(String phone, String text) {

  SmsManager smsManager = SmsManager.getDefault();

  Class clazz = smsManager.getClass();

  try {

   Class[] types = new Class[4];
   types[0] = byte[].class;
   types[1] = byte[].class;
   types[2] = Class.forName("android.app.PendingIntent");
   types[3] = Class.forName("android.app.PendingIntent");

   Object[] params = new Object[4];
   params[0] = null;
   params[1] = getSubmitPdu(null, phone, text).encodedMessage;
   params[2] = PendingIntent
     .getBroadcast(mContext, 0, new Intent(), 0);
   // params[2] = null;
   params[3] = null;

   Method m = clazz.getDeclaredMethod("sendRawPdu", types);
   m.setAccessible(true);
   m.invoke(smsManager, params);

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }
 }

 public static SubmitPdu getSubmitPdu(String scAddress,
   String destinationAddress, String message) {

  if (message == null || destinationAddress == null) {
   return null;
  }

  SubmitPdu ret = new SubmitPdu();

  byte mtiByte = (byte) (0x01 | 0x00);

  ByteArrayOutputStream bo = getSubmitPduHead(scAddress,
    destinationAddress, mtiByte, false, ret);

  byte[] userData, textPart;
  // Encoding to the 7-bit alphabet failed. Let's see if we can
  // send it as a UCS-2 encoded message

  try {
   textPart = message.getBytes("utf-16be");
  } catch (UnsupportedEncodingException uex) {
   uex.printStackTrace();
   return null;
  }

  userData = textPart;

  if (userData.length > MAX_USER_DATA_BYTES) {
   // Message too long
   return null;
  }

  // TP-Data-Coding-Scheme
  // Class 3, UCS-2 encoding, uncompressed
  bo.write(0x0b);

  // (no TP-Validity-Period)

  // TP-UDL
  bo.write(userData.length);

  bo.write(userData, 0, userData.length);

  ret.encodedMessage = bo.toByteArray();

  return ret;
 }

 private static ByteArrayOutputStream getSubmitPduHead(String scAddress,
   String destinationAddress, byte mtiByte,
   boolean statusReportRequested, SubmitPdu ret) {

  ByteArrayOutputStream bo = new ByteArrayOutputStream(
    MAX_USER_DATA_BYTES + 40);

  // SMSC address with length octet, or 0
  if (scAddress == null) {
   ret.encodedScAddress = null;
  } else {
   ret.encodedScAddress = PhoneNumberUtils
     .networkPortionToCalledPartyBCDWithLength(scAddress);
  }

  // TP-Message-Type-Indicator (and friends)
  if (statusReportRequested) {
   // Set TP-Status-Report-Request bit.
   mtiByte |= 0x20;
  }
  bo.write(mtiByte);

  // space for TP-Message-Reference
  bo.write(0);

  byte[] daBytes;

  daBytes = PhoneNumberUtils
    .networkPortionToCalledPartyBCD(destinationAddress);

  // destination address length in BCD digits, ignoring TON byte and pad
  // TODO Should be better.
  bo.write((daBytes.length - 1) * 2
    - ((daBytes[daBytes.length - 1] & 0xf0) == 0xf0 ? 1 : 0));

  // destination address
  bo.write(daBytes, 0, daBytes.length);

  // TP-Protocol-Identifier
  bo.write(0);

  return bo;
 }

}
标签:

Posted by hee at 21:12 PM | Permalink | 评论(3) | Android

5684 天 13 小时前
沙发 billy 说:

经过测试,在模拟器上发送是不成功的。

收到的是乱码。

模拟器是1.5和1.1的,1.0没测。

不知你是否在模拟器上测试过?

5684 天 13 小时前
板凳 billy 说:

理论上来讲直接通过pdu来发送是没问题,不过pdu如何创建才不是乱码,这个就不容易了。

5679 天 15 小时前
地板 hee 说:

模拟器上有没有测试过,现在都不敢确定了。

但真机(G1 RC33)上测试过,可以正常发送中文。

1.5里,不是说已经可以发非英文字符了吗?

请输入名称
请输入邮件地址

 

    请输入邮件地址