读dumpstate数据demo
SystemProperties.set("ctl.stop", "dumpstate");
SystemProperties.set("ctl.start", "dumpstate");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalSocket localSocket = new LocalSocket();
LocalSocketAddress localSocketAddress =
new LocalSocketAddress("dumpstate", LocalSocketAddress.Namespace.RESERVED);
try {
localSocket.connect(localSocketAddress);
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream inputStream = localSocket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/bugreport.txt");
int readLen = 0;
while (readLen != -1) {
byte[] bytes = new byte[65535];
readLen = inputStream.read(bytes);
String string = new String(bytes);
if (readLen > 0) {
fileOutputStream.write(bytes, 0, readLen);
// Log.e(TAG, "PZC " + string);
}
}
inputStream.close();
localSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
SystemProperties.set("ctl.stop", "dumpstate");
Log.e(TAG, "PZC dump end");
}
}).start();
容易出错的地方:
while (readLen != -1) {
byte[] bytes = new byte[65535];
readLen = inputStream.read(bytes);
String string = new String(bytes);
if (readLen > 0) {
fileOutputStream.write(bytes, 0, readLen);
// Log.e(TAG, "PZC " + string);
}
}
别写成下边的形式,之前发现老有重复数据,排查了一圈,发现自己写重复了。。。。
byte[] bytes = new byte[65535];
inputStream = localSocket.getInputStream();
fileOutputStream = new FileOutputStream(path);
while (inputStream.read(bytes) != -1) {
fileOutputStream.write(bytes);
}