編輯:關於Android編程
讓logcat實時的輸出保存到某個目錄下,我們基本都知道可以通過adb命令來實現,那麼怎麼通過代碼實現呢?這是本篇博文要說的內容。
我這裡是把log保存到了sdcard的根目錄下,不羅嗦了,上代碼。
[java]
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.os.Environment;
import android.util.Log;
public class WriteLog {
private static final String TAG = "Log";
// LogWrite
private String LOG_PATH_SDCARD_DIR; // log file path in sdcard
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// log
// name
private Process process;
private static WriteLog mLogDemo = null;
private WriteLog() {
init();
}
public static WriteLog getInstance() {
if (mLogDemo == null) {
mLogDemo = new WriteLog();
}
return mLogDemo;
}
public void startLog() {
createLog();
}
public void stopLog() {
if (process != null) {
process.destroy();
}
}
private void init() {
LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory()
.getAbsolutePath();
createLogDir();
Log.i(TAG, "Log onCreate");
}
/**
* write the log
*/
public void createLog() {
// TODOWriteLog
List<String> commandList = new ArrayList<String>();
commandList.add("logcat");
commandList.add("-f");
commandList.add(getLogPath());
commandList.add("-v");
commandList.add("time");
try {
process = Runtime.getRuntime().exec(
commandList.toArray(new String[commandList.size()]));
} catch (Exception e) {
Log.e(TAG,e.getMessage(), e);
}
}
/**
* the path of the log file
*
* @return
*/
public String getLogPath() {
createLogDir();
String logFileName = sdf.format(new Date()) + ".log";// name
Log.d(TAG, "Log stored in SDcard, the path is:" + LOG_PATH_SDCARD_DIR
+ File.separator + logFileName);
return LOG_PATH_SDCARD_DIR + File.separator + logFileName;
}
/**
* make the dir
*/
private void createLogDir() {
File file;
boolean mkOk;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
file = new File(LOG_PATH_SDCARD_DIR);
if (!file.isDirectory()) {
mkOk = file.mkdirs();
if (!mkOk) {
return;
}
}
}
}
}
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.os.Environment;
import android.util.Log;
public class WriteLog {
private static final String TAG = "Log";
// LogWrite
private String LOG_PATH_SDCARD_DIR; // log file path in sdcard
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// log
// name
private Process process;
private static WriteLog mLogDemo = null;
private WriteLog() {
init();
}
public static WriteLog getInstance() {
if (mLogDemo == null) {
mLogDemo = new WriteLog();
}
return mLogDemo;
}
public void startLog() {
createLog();
}
public void stopLog() {
if (process != null) {
process.destroy();
}
}
private void init() {
LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory()
.getAbsolutePath();
createLogDir();
Log.i(TAG, "Log onCreate");
}
/**
* write the log
*/
public void createLog() {
// TODOWriteLog
List<String> commandList = new ArrayList<String>();
commandList.add("logcat");
commandList.add("-f");
commandList.add(getLogPath());
commandList.add("-v");
commandList.add("time");
try {
process = Runtime.getRuntime().exec(
commandList.toArray(new String[commandList.size()]));
} catch (Exception e) {
Log.e(TAG,e.getMessage(), e);
}
}
/**
* the path of the log file
*
* @return
*/
public String getLogPath() {
createLogDir();
String logFileName = sdf.format(new Date()) + ".log";// name
Log.d(TAG, "Log stored in SDcard, the path is:" + LOG_PATH_SDCARD_DIR
+ File.separator + logFileName);
return LOG_PATH_SDCARD_DIR + File.separator + logFileName;
}
/**
* make the dir
*/
private void createLogDir() {
File file;
boolean mkOk;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
file = new File(LOG_PATH_SDCARD_DIR);
if (!file.isDirectory()) {
mkOk = file.mkdirs();
if (!mkOk) {
return;
}
}
}
}
}
使用的是單例模式,使用方法也很簡單,如下:
[java]
WriteLog log = WriteLog.getInstance();
log.startLog();//開始保存log
WriteLog log = WriteLog.getInstance();
log.startLog();//開始保存log在你調起以後它就可以實時的去保存log至我們指定的路徑,如果需要停止log的保存輸出就需要調用stopLog()方法。
學習目的: 1、掌握在Android中如何建立Gallery 2、初步理解Android適配器的原理 3、實現簡單的控件縮放動畫 簡介: 1、Gallery是Androi
Android現在實現Tab類型的界面方式越來越多,今天就把常見的實現方式給大家來個總結。目前寫了:1、傳統的ViewPager實現2、FragmentManager+F
本文實例講述了Android實現的截屏小程序。分享給大家供大家參考,具體如下:先看截圖,不過這個截屏還不夠完整,頭上的statusbar沒有,呈黑色。多按了幾次,就成這樣
本節引言: 上一節,我們學習了Xfermode中的三兒子:PorterDuffXfermode構造方法中的為一個參數: PorterDuf