`
abc20899
  • 浏览: 907769 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
有的时候我们调试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);
                 }
             }
         }
     }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics