1.ReadLog.java
package com.android.ReadLog;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.cw.utils.FileUtils;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
public class ReadLog {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
public static final String ECHO_ROOT = "echo root";
public Context mContext;
static FileUtils fileUtils = new FileUtils();
private static final String file = Environment
.getExternalStorageDirectory() + "/fileName.txt";
private static final String file1 = Environment
.getExternalStorageDirectory() + "/fileName1.txt";
private static final String file2 = Environment
.getExternalStorageDirectory() + "/fileName2.txt";
private static final String file3 = Environment
.getExternalStorageDirectory() + "/fileName3.txt";
private static final String Tag = "AdbShellUtil";
public ReadLog(Context mContext) {
this.mContext = mContext;
}
/**
* check whether has root permission ����Ƿ���rootȨ��
*
* @return
*/
public boolean checkRootPermission() {
return execCommand(ECHO_ROOT, true, false).result == 0;
}
public CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] { command }, isRoot, true);
}
@SuppressWarnings("rawtypes")
public CommandResult execCommand(List commands, boolean isRoot) {
@SuppressWarnings("unchecked")
String[] array = (String[]) commands.toArray(new String[] {});
return execCommand(commands == null ? null : array, isRoot, true);
}
public CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}
public CommandResult execCommand(String command, boolean isRoot,
boolean isNeedResultMsg) {
return execCommand(new String[] { command }, isRoot, isNeedResultMsg);
}
@SuppressWarnings("rawtypes")
public CommandResult execCommand(List commands, boolean isRoot,
boolean isNeedResultMsg) {
@SuppressWarnings("unchecked")
String[] array = (String[]) commands.toArray(new String[] {});
return execCommand(commands == null ? null : array, isRoot,
isNeedResultMsg);
}
public CommandResult execCommand(String[] commands, boolean isRoot,
boolean isNeedResultMsg) {
// ��һ��
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Runtime runtime = null;
Process process = null;
BufferedReader reader = null;
StringBuffer respondBuff = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
StringBuilder currentCommand = null;
// DataOutputStream os = null;
try {
currentCommand = new StringBuilder();
for (String command : commands) {
if (command == null) {
continue;
} else {
currentCommand.append(command);
currentCommand.append(COMMAND_LINE_END);
}
}
ArrayList<String> clearLog = new ArrayList<String>();
clearLog.add("logcat");
clearLog.add("-c");
runtime = Runtime.getRuntime();
process = runtime.exec(currentCommand.toString());
respondBuff = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int logCount = 0;
while ((line = reader.readLine()) != null) {// 每次读取一行
logCount++;
fileUtils.method1(file, line + "\r\n");
//Log.i(Tag, "line的内容" + line + "\r\n");
// 页面开始加载
if (line.contains("I/chromium") && line.contains("Started")) {
fileUtils.method1(file2, line + "\r\n");
Intent loadstartIntent = new Intent();
loadstartIntent
.setAction("com.cw.browserRate.loadUrl_start");
mContext.sendBroadcast(loadstartIntent);
Log.i(Tag, "--start---");
}
// 页面加载结束:
if (line.contains("I/chromium")
&& line.contains("Browser Finished Loading URL: https:")) {
fileUtils.method1(file1, line + "\r\n");
Log.i(Tag, "--fason---");
Intent loadfinishIntent = new Intent();
loadfinishIntent
.setAction("com.cw.browserRate.loadUrl_finished");
mContext.sendBroadcast(loadfinishIntent);
Log.i(Tag, "--fason1---");
}
// 页面加载失败
if (line.contains("I/chromium")
&& line.contains(" Browser Finished Loading URL: data:text/html,chromewebdata")) {
Intent errorIntent = new Intent();
errorIntent.setAction("com.cw.browserRate.loadUrl_error");
mContext.sendBroadcast(errorIntent);
}
if (logCount > 512) {
Runtime.getRuntime()
.exec(clearLog.toArray(new String[clearLog.size()]))
.destroy();// 清除日志
logCount = 0;
Log.i(Tag, "-----------clearlog--------");
}
}
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null
: successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
public static class CommandResult {
/** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}