private static final String TAG = "LogDumperActivity";
private Button startLogBtn, stopLogBtn;
/**
* debug模式文件夹所在位置
*/
private String debugFilePath = Environment.getExternalStorageDirectory()
.toString() + "/logdump/";
/**
* 日志清除命令
*/
private String clearCommand = "logcat -c";
/**
* 日志重定向命令
*/
private String logToFileCommand = "logcat -v time -f ";
/**
* 日志输出进程
*/
private Process logProcess = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startLogBtn = (Button) findViewById(R.id.start_log_btn);
stopLogBtn = (Button) findViewById(R.id.stop_log_btn);
startLogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
createLogFolder();
startLogging();
}
});
stopLogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
killProcess("logcat");
}
});
}
/**
* Debug模式初始化<BR>
* [功能详细描述]
*/
private void startLogging() {
killProcess("logcat");
try {
// 先将日志清除
Runtime.getRuntime().exec(clearCommand);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String nowStr = format.format(new Date());
String fileName = "log_" + nowStr + ".txt";
// 打印版本手机信息
String platformStr = "Android";
String sdkVersion = android.os.Build.VERSION.RELEASE;
String deviceStr = android.os.Build.DEVICE;
Log.i("platform:", platformStr);
Log.i("SDK version:", sdkVersion);
Log.i("device name:", deviceStr);
// 重定向日志
logProcess = Runtime.getRuntime().exec(
logToFileCommand + debugFilePath + fileName);
} catch (IOException e) {
Log.e(TAG, "initDebugMode() " + e.toString());
}
}
public static void killProcess(String processName) {
Process process = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
process = Runtime.getRuntime().exec("ps " + processName);
int myPid = android.os.Process.myPid();
// pid所在位置
int pIdPosition = -1;
// ppid所在位置
int ppIdpostion = -1;
if (null != process) {
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String commandRusult = "";
String pIdStr = "PID";
String ppIdStr = "PPID";
int i = 0;
while (null != commandRusult) {
commandRusult = br.readLine();
if (null == commandRusult) {
break;
}
// 以多一个或多个空格分割
String[] processes = commandRusult.split(" +");
if (null != processes && processes.length > 0) {
// 第一行分析PID 以及PPID的位置
if (i == 0) {
for (int j = 0; j < processes.length; j++) {
Log.d(TAG, "processes[j]" + processes[j]);
// 找到PID的位置
if (pIdStr.equalsIgnoreCase(processes[j])) {
pIdPosition = j;
}
// 找到PPID的位置
else if (ppIdStr.equalsIgnoreCase(processes[j])) {
ppIdpostion = j;
}
if (pIdPosition > 0 && ppIdpostion > 0) {
break;
}
}
// 这个方法没办法执行了 找不到PID 与PPID
if (pIdPosition < 0 || ppIdpostion < 0) {
return;
}
} else {
int ppid = Integer.parseInt(processes[ppIdpostion]);
// 杀掉这个进程
if (ppid == myPid || 1 == ppid) {
int pid = Integer
.parseInt(processes[pIdPosition]);
Log.d(TAG, ppid + " = ppid" + pid + " = pid");
android.os.Process.killProcess(pid);
}
}
}
i++;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
br = null;
}
if (null != isr) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
isr = null;
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (null != process) {
process.destroy();
process = null;
}
}
}
private void createLogFolder() {
File debugFile = new File(debugFilePath);
if (debugFile.exists()) {
return;
} else {
debugFile.mkdirs();
}
}