说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

帮助ADT改进DDMS中的Logcat中文乱码问题

HTML文档下载 WORD文档下载 PDF文档下载
帮助ADT改进DDMS中的Logcat中文乱码问题

作者:Android开发网


  有的时候我们调试Android应用可能涉及中文内容,但是在DDMS的Logcat下显示中文时为乱码,这里大家可以通过自己编译SDK来解决,有关编译Android SDK方法可以参考如何编译Windows平台的Android SDK 下面一起看下哪个代码存在问题吧。
  在Android源码DDMS中我们找到 MultiLineReceiver 这个类,对应GIT开源在development/tools/ddms/libs/ddmuilib/src/com/android/ddmuilib/,最主要的就是有关String实例化时最后一个参数,看到ISO-8859-1了吧,我们将这个换成gb2312就可以很好的显示简体中文了,繁体嘛可以考虑big5这种编码等等了,当然了Android123推荐大家使用UTF-8这种兼容性最好的。

 public abstract class MultiLineReceiver implements IShellOutputReceiver {

     public final void addOutput(byte[] data, int offset, int length) {
         if (isCancelled() == false) {
             String s = null;
             try {
                 s = new String(data, offset, length, "ISO-8859-1"); //问题在这里,ISO-8859-1就是Latin-1我们俗称西欧语言
             } catch (UnsupportedEncodingException e) {
                 // normal encoding didn't work, try the default one
                 s = new String(data, offset,length);
             }

             // ok we've got a string
             if (s != null) {
                 // if we had an unfinished line we add it.
                 if (mUnfinishedLine != null) {
                     s = mUnfinishedLine + s;
                     mUnfinishedLine = null;
                 }


                 mArray.clear();
                 int start = 0;
                 do {
                     int index = s.indexOf("\r\n", start);

                     // if \r\n was not found, this is an unfinished line
                     // and we store it to be processed for the next packet
                     if (index == -1) {
                         mUnfinishedLine = s.substring(start);
                         break;
                     }


                     String line = s.substring(start, index);
                     if (mTrimLines) {
                         line = line.trim();
                     }
                     mArray.add(line);

                     // move start to after the \r\n we found
                     start = index + 2;
                 } while (true);

                 if (mArray.size() > 0) {

                     String[] lines = mArray.toArray(new String[mArray.size()]);

                     // send it for final processing
                     processNewLines(lines);
                 }
             }
         }
     }
 }
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘