1. 程式人生 > >android基礎--獲取logcat資訊

android基礎--獲取logcat資訊

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

        }

   }