帮助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);
                 }
             }
         }
     }
 }

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号