- if (android.os.Build.MODEL.equals("meizu_m9")){
- System.out.println("我是M9手机");
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.check);
- /**得到这个按钮对象**/
- button = (Button)findViewById(R.id.button0);
- /**监听这个按钮**/
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- /**输出一段Log信息**/
- Log.i("Mytest", "this is a test");
- /**开启线程用于监听log输出的信息**/
- new Thread(CheckActivity.this).start();
- }
- });
- }
- @Override
- public void run() {
- Process mLogcatProc = null;
- BufferedReader reader = null;
- try {
- //获取logcat日志信息
- mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat","Mytest:I *:S" });
- reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
- String line;
- while ((line = reader.readLine()) != null) {
- if (line.indexOf("this is a test") > 0) {
- //logcat打印信息在这里可以监听到
- // 使用looper 把给界面一个显示
- Looper.prepare();
- Toast.makeText(this, "监听到log信息", Toast.LENGTH_SHORT).show();
- Looper.loop();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- <uses-permission android:name="android.permission.READ_LOGS" />
效果图
最后还是老规矩每篇文章都会附带源代码,最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。
下载地址:http://down.51cto.com/data/259343
下载地址:http://down.51cto.com/data/259343