1.直接贴代码
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import com.nuotu.atmBookClient.App;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyCrashHandler implements UncaughtExceptionHandler {
private static MyCrashHandler instance;
String result;
public static MyCrashHandler getInstance() {
if (instance == null) {
instance = new MyCrashHandler();
}
return instance;
}
private Thread.UncaughtExceptionHandler mDefaultHandler;
public void init(Context ctx) {
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {
Log.d("111333","打印报错信息="+arg1.getMessage());
if (handleException(arg1)) {
} else {
if (mDefaultHandler != null) {
mDefaultHandler.uncaughtException(arg0, arg1);
}
}
String logPath;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
logPath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ File.separator
+ "错误日志Log";
Log.d("1113332","打印报错信息文件的存放路径="+logPath);
File file = new File(logPath);
if (!file.exists()) {
file.mkdirs();
}
try {
FileWriter fw = new FileWriter(logPath + File.separator
+ "myErrorlog.log", true);
StackTraceElement[] stackTrace = arg1.getStackTrace();
fw.write(getNowTime());
fw.write("\n========================这里存放最重要的错误信息提示,具体到哪一行报错(开始)===========================\n");
fw.write(result);
fw.write("\n========================这里存放最重要的错误信息提示,具体到哪一行报错(结束)===========================\n");
fw.close();
} catch (IOException e) {
Log.e("crash handler", "load file failed...", e.getCause());
}
}
arg1.printStackTrace();
}
private boolean handleException(Throwable e) {
if (e == null) {
return false;
}
Writer writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
e.printStackTrace(pw);
pw.close();
result = writer.toString();
Log.d("111333", "打印出错误日志================================(开始)\n"+result+"打印出错误日志================================(结束)\n");
return true;
}
private void restartApp() {
Intent intent = App.getContext().getPackageManager()
.getLaunchIntentForPackage(App.getContext().getPackageName());
PendingIntent restartIntent = PendingIntent.getActivity(App.getContext(), 0, intent, 0);
AlarmManager mgr = (AlarmManager) App.getContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
System.exit(0);
}
private static String getNowTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
}
}
2.在Application中进行初始化
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MyCrashHandler myCrashHandler = MyCrashHandler.getInstance();
myCrashHandler.init(getApplicationContext());
TextView t=null;
t.setText("");
}
}
3.修改mainfests,添加权限,修改application节点。需要配置全局启动类,不然不会启动MyApplication 类。还有需要注意,如果是安卓6.0以上的系统需要动态申请权限(读写文件权限)。
就是图片中的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yu.myapplication">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>