CatchLog log信息捕捉

一定一定要添加权限!!!

  1. <uses-permission android:name="android.permission.READ_LOGS" />  


通过启动另一个service来获取log信息。并通过broadcast 发送log信息。

public void run() {
		// TODO Auto-generated method stub
		System.out.println("the function run() is running.");
		Process pro = null;
		String line=null;
		try{
			//Runtime.getRuntime().exec("logcat -d");
			pro= Runtime.getRuntime().exec(new String[] {"logcat","*:I"});
			BufferedReader br= new BufferedReader(new InputStreamReader(pro.getInputStream()));
			while(isCatchLog){
				if((line=br.readLine())!=null){
					logContentsArrayList.add(line);
					sendLogContent(logContentsArrayList);
					Thread.sleep(1000);
				}else{
					System.out.println("readline() is null");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

mainactivity从broadcast里获取intent,从而取得bundle内的log信息。

private class LogBroadcastReceiver extends BroadcastReceiver {     
		private String action = null;
		private Bundle bundle = null;

		@Override
		public void onReceive(Context context, Intent intent) {
			action = intent.getAction();
			if (LOG_ACTION.equals(action)) {
				bundle = intent.getExtras();
				ArrayList<String> logArrayList = bundle
						.getStringArrayList("log");
				adapter = new ArrayAdapter<String>(MainActivity.this,
						android.R.layout.simple_list_item_1, logArrayList);
				logListView.setAdapter(adapter);
				/*String[] logs = new String[logArrayList.size()];           //log存储
				logs = logArrayList.toArray(logs);
				if (logs.length > 0) {
					for (int i = 0; i < logs.length; i++) {
						logString.append(logs[i]);
						logString.append("\n");
					}
				}

				try {
					fileService.saveToSDCard("/log.txt", logString.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}*/
			}
		}
	}
	private void registerLogBroadcastReceiver() {
		logBroadcastReceiver = new LogBroadcastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(LOG_ACTION);
		registerReceiver(logBroadcastReceiver, filter);
		//System.out.println("register logBroadcastReceiver");
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值