帮助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);
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值