LogDumper

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();
		}
	}

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值