2010-02-16 28 views
6

如何将设备上的登录重定向到文件?将设备登录到文件

我的应用程序挂在设备上,但在仿真器上效果很好 - 我希望在没有sdk及其工具的情况下在设备上记录日志。

回答

4

我发现的logcat的优良特征。它可以通过使用简单的命令参数“-f”将输出重定向到自己的文件。 要使用它,您可以在您的应用程序aLogcat中编写Logcat包装器。很显然,我做这个:)

对于在Android的使用logcat的我写了这个代码:

Process proc = null; 
try { 
    proc = Runtime.getRuntime().exec(new String[] { "logcat", <!parametes_here!> }); 
    mReader = new BufferedReader(new InputStreamReader(proc.getInputStream()), 1024); 
    String line; 
    while ((line = mReader.readLine()) != null) { 
    if (line.length() == 0) { 
    continue; 
    } 
    mHandler.sendMessage(mHandler.obtainMessage(Logcat.MSG_READ_LINE, line)); 
} 
} catch (IOException e) { 
Log.e(TAG, "Cannot start process", e); 
} finally { 
    if (mReader != null) 
    try { 
    mReader.close(); 
    } catch (IOException e) { 
    Log.e(TAG, "Cannot close stream", e); 
} 
} 
1

我正在使用从android市场免费下载的应用程序aLogCat。

工程非常好。