在Android界面上显示和获取Logcat日志输出的方法 |
您所在的位置:网站首页 › adb调试软件 › 在Android界面上显示和获取Logcat日志输出的方法 |
一、首先我们要获取Logcat中的日志 如何获取呢? 首先我们要先定义一个String[]数组,里面的代码是 //第一个是Logcat ,也就是我们想要获取的log日志 //第二个是 -s 也就是表示过滤的意思 //第三个就是 我们要过滤的类型 W表示warm ,我们也可以换成 D :debug, I:info,E:error等等 String[] running = new String[]{"logcat","-s","adb logcat *: W"};当我们设置好之后,我们还需要一个process类,作用通俗来讲就是用Java代码来进行adb命令行操作代码是: Process exec = Runtime.getRuntime().exec(running);通过以上的方法我们就可以获得和过滤Logcat中的方法。 二、接下来开始使用IO流进行字符操作,把数据保存在Android SDCard中 首先:我们定义一个InputStream, final InputStream is = exec.getInputStream接下来开启一个线程,线程中的方法就是通过IO流先读取Logcat中的数据,然后再把数据通过OutPutStream方法写入到SDCard中。 new Thread() { @Override public void run() { FileOutputStream os = null; try { //新建一个路径信息 os = new FileOutputStream("/sdcard/Log/Log.txt"); int len = 0; byte[] buf = new byte[1024]; while (-1 != (len = is.read(buf))) { os.write(buf, 0, len); os.flush(); } } catch (Exception e) { Log.d("writelog", "read logcat process failed. message: " + e.getMessage()); } finally { if (null != os) { try { os.close(); os = null; } catch (IOException e) { // Do nothing } } } } }.start(); } catch (Exception e) { Log.d("writelog", "open logcat process failed. message: " + e.getMessage()); } }当我们这个类写完之后,我们再把权限添加进去就可以了。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |