Android画面でログ出力を表示して取得する
Android画面でログ出力を表示して取得する AndroidインタフェースでLogcatログ出力を表示および取得 1、まずLogcatのログ を取得します.二、次にIOストリームによる文字操作を開始し、Android SDCardにデータを保存する 三、その後、まずページを作成し、Txtテキストの内容 を行単位で読み出す.最後、クリアログ
一、まずLogcatのログを取得する
どのように取得しますか?
まず
設定が完了すると、
以上の方法でLogcatの方法を得ることができ,フィルタリングすることができる.
二、次にIOストリームによる文字操作を開始し、
まず
次にスレッドが開き、スレッド内の方法はIOストリームを介して
このクラスを書き終わったら、権限を追加すればいいです.
権限を追加したら、実行してみましょう.
次に、
これにより、
2回開いたので2回印刷した
三、その後、まずページを作成してから、Txtテキストの内容を行ごとに読み取ります.
まず、
作成が完了すると、
その後、
dのコードを見ると
その後、
最後に、
最後に
次に
では、効果を見てみましょう.
はい、私たちの表示ログも成功しました.次に、ログを空にすることができます.
最後に、ログをクリア
ログをクリアするにはどうすればいいですか?
実はとても簡単です
パスを渡して判断し、あれば直接削除するだけです.
そして
一、まず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()) //
その後、
InputStreamReader
でSDCard
のファイルを読み取ります.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"
/>
次に
List
をListView
に入れます. 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
をリフレッシュすればいいです.