要加入权限
<uses-permission android:name="android.permission.READ_LOGS" />
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Demo8Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
String filenameTemp = Environment.getExternalStorageDirectory().toString()+ "/app.txt";// 照片文件绝对路径
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//例子
try
{
int i=10/0;
}catch(Exception e)
{
Log.e("Android", e.getMessage());
}
Process mLogcatProc = null;
BufferedReader reader = null;
try
{
mLogcatProc = Runtime.getRuntime().exec(new String[]
{"logcat", "-d", "AndroidRuntime:E Android:V *:S" });//Android为你的Tag
reader = new BufferedReader(new InputStreamReader
(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
CreateText();
print(log.toString());
// do whatever you want with the log. I'd recommend using Intents to create an email
}
catch (IOException e)
{
}
finally
{
if (reader != null)
try
{
reader.close();
}
catch (IOException e)
{
}
}
}
});
}
//创建文件夹及文件
public void CreateText() throws IOException {
File dir = new File(filenameTemp);
if (!dir.exists()) {
try {
//在指定的文件夹中创建文件
System.out.println(dir.createNewFile()+"---------------------");;
} catch (Exception e) {
}
}
}
//向已创建的文件中写入数据
public void print(String str) {
FileWriter fw = null;
BufferedWriter bw = null;
String datetime = "";
try {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
+ "hh:mm:ss");
datetime = tempDate.format(new java.util.Date()).toString();
fw = new FileWriter(filenameTemp, true);//
// 创建FileWriter对象,用来写入字符流
bw = new BufferedWriter(fw); // 将缓冲对文件的输出
String myreadline = datetime + "[]" + str;
bw.write(myreadline + "\n"); // 写入文件
bw.newLine();
bw.flush(); // 刷新该流的缓冲
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
bw.close();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
/**
* 监听退出事件
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// 确认对话框
final AlertDialog isExit = new AlertDialog.Builder(this).create();
// 对话框标题
isExit.setTitle("系统提示");
// 对话框消息
isExit.setMessage("确定要退出吗");
// 实例化对话框上的按钮点击事件监听
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case AlertDialog.BUTTON1:// "确认"按钮退出程序
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
break;
case AlertDialog.BUTTON2:// "取消"第二个按钮取消对话框
isExit.cancel();
break;
default:
break;
}
}
};
// 注册监听
isExit.setButton("确定", listener);
isExit.setButton2("取消", listener);
// 显示对话框
isExit.show();
return false;
}
return false;
}
}
日志输出到文件是
:
2012-02-28 05:06:00[]--------- beginning of /dev/log/system--------- beginning of /dev/log/mainE/AndroidRuntime(22045): divide by zero
关于logcat 命令的用法可以查看
http://developer.android.com/guide/developing/tools/adb.html#usinglogcat
Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at priority "Info" or above, and all log messages with tag "MyApp", with priority "Debug" or above:
adb logcat ActivityManager:I MyApp:D *:S
分享到:
相关推荐
Android Gps日志记录程序源码
AndroidGps日志记录程序源码.zip
Android 基础开发 Gps日志记录程序源码 个人觉得还是比较完善的
Android程序研发源码Android Gps日志记录程序源码.rar
Android Gps日志记录程序源码.zip源码资源下载Android Gps日志记录程序源码.zip源码资源下载
Android Gps日志记录程序源码,真实有效的不错源码,本人不要积分但求好评,谢谢大家!希望多关注。
Android Gps日志记录程序源码.zip
Android Gps日志记录程序源码.zip项目安卓应用源码下载Android Gps日志记录程序源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android源码——Gps日志记录程序源码.zip
安卓Android源码——Gps日志记录程序源码.zip
Android源码——Gps日志记录程序源码.7z
android日志分析&崩溃拦截,包括如下: andorid手机程序错误信息的demo.rar android app异常收集处理.rar android java 通用代码,关于用properties... 日志记录,开源项目使用方法见说明.zip 用户反馈小demo.zip
Android应用源码时光日志本一句话记录每一天
APP测试-Android日志分析1....2.所谓的日志,其实是一种记录机制,允许我们在程序代码中插入一些特殊的输出代码,将程序当前的运行状态随时输出,以便于在无人值守的情况下记录信息,在事后对程序的处理过程进行分析。