Android画面でログ出力を表示して取得する


Android画面でログ出力を表示して取得する
  • AndroidインタフェースでLogcatログ出力を表示および取得
  • 1、まずLogcatのログ
  • を取得します.
  • 二、次にIOストリームによる文字操作を開始し、Android SDCardにデータを保存する
  • 三、その後、まずページを作成し、Txtテキストの内容
  • を行単位で読み出す.
  • 最後、クリアログ

  • 一、まず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());
            }
        }

    このクラスを書き終わったら、権限を追加すればいいです.
        
        <uses-permission android:name="android.permission.READ_LOGS" />
        
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
        
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    権限を追加したら、実行してみましょう.
    次に、SDCardのファイルディレクトリを開きます.
    これにより、Logcatのログが取得されました(コンソールと比較できます):
    2回開いたので2回印刷したlog.
    三、その後、まずページを作成してから、Txtテキストの内容を行ごとに読み取ります.
    まず、XMlビューファイルの作成を開始します.
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <LinearLayout 
                android:layout_width="match_parent"
                android:layout_weight="7"
                android:orientation="vertical"
            >
            <ListView 
                android:id="@+id/ListLog"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >ListView>
    
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >
    
            <Button 
            android:layout_gravity="center"
                android:gravity="center"
                android:id="@+id/BtnLog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="    "
                />
    
        LinearLayout>
    
    LinearLayout>
    

    作成が完了すると、MainActivityでクラスを初期化し始めました.
    private ListView listView;
        private Button btn;
    
    
       listView = (ListView) findViewById(R.id.ListLog);
       btn = (Button) findViewById(R.id.BtnLog);
    
    

    その後、TXTファイルを読み込む方法を書き始めました.
    /** 
         *         
         * @return 
         */  
        public List Txt() {    
            //            List      
            String filePath = "/sdcard/Log.txt";    
    
            List newList=new ArrayList();  
            try {    
                File file = new File(filePath);    
                int count = 0;//    key     
                if (file.isFile() && file.exists()) {//        
                    InputStreamReader isr = new InputStreamReader(new FileInputStream(file));    
                    BufferedReader br = new BufferedReader(isr);    
                    String lineTxt = null;    
                    while ((lineTxt = br.readLine()) != null) {  
                        if (!"".equals(lineTxt)) {    
                            String reds = lineTxt.split("\\+")[0];  //java          
                            newList.add(count, reds);  
                            count++;    
                        }    
                    }    
                    isr.close();    
                    br.close();    
                }else {    
                    Log.e("tag", "can not find file");
                }    
            } catch (Exception e) {    
                e.printStackTrace();    
            }    
            return newList;    
        }    

    dのコードを見るとIO です
     if (file.isFile() && file.exists()) //             

    その後、InputStreamReaderSDCardのファイルを読み取ります.BufferedReaderメソッドを使用して、取得した文字ストリームを読み出します.
    最後に、Whileサイクルと正規表現を使用して、各行をリストに入れます.
    最後にListに戻ります.
      InputStreamReader isr = new InputStreamReader(new FileInputStream(file));    
                    BufferedReader br = new BufferedReader(isr);    
                    String lineTxt = null;    
                    while ((lineTxt = br.readLine()) != null) {  
                        if (!"".equals(lineTxt)) {    
                            String reds = lineTxt.split("\\+")[0];  //java          
                            newList.add(count, reds);  
                            count++;    
                        }    
                    }    
    XMLビューファイル、名前log_list_item.xml
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
         android:textColor="#000000"
        android:gravity="left"
        android:paddingLeft="20dp"
        android:textSize="20sp"
        android:singleLine="true"
    
    
    />

    次にListListViewに入れます.
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt());
     listView.setAdapter(adapter);

    では、効果を見てみましょう.
    はい、私たちの表示ログも成功しました.次に、ログを空にすることができます.
    最後に、ログをクリア
    ログをクリアするにはどうすればいいですか?
    実はとても簡単です
      /**
         *   Log  
         * @param fileName        
         */
        public static void delFile(String fileName){  
            File file = new File(fileName);  
            if(file.isFile()){  
                file.delete();  
            }  
            file.exists();  
        }  

    パスを渡して判断し、あれば直接削除するだけです.
    そしてListViewをリフレッシュすればいいです.