捕获错误和用户信息 [英] Catching error and user information
本文介绍了捕获错误和用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,因为我的应用程序在后台运行,有时会出现一个错误,将其关闭。如何捕捉任何活动或服务的错误,以播放声音和通知栏上添加信息?
I have a problem because my application runs in the background and sometimes an error occurs that closes it. How to catch an error in any activity or service in order to play a sound and add info on the notification bar?
推荐答案
要解决这个问题,你可以使用的UncaughtExceptionHandler接口这一点。
To solve this problem, you can use uncaughtExceptionHandler interface for this.
public class ExceptionHandler implements UncaughtExceptionHandler{
Utils utils ;
Context context;
private final String LINE_SEPARATOR = "\n";
public ExceptionHandler(Context con) {
// TODO Auto-generated constructor stub
context = con;
}
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {
// TODO Auto-generated method stub
StringWriter stackTrace = new StringWriter();
arg1.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
errorReport.append("Brand: ");
errorReport.append(Build.BRAND);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Device: ");
errorReport.append(Build.DEVICE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Model: ");
errorReport.append(Build.MODEL);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Id: ");
errorReport.append(Build.ID);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Product: ");
errorReport.append(Build.PRODUCT);
errorReport.append(LINE_SEPARATOR);
errorReport.append("\n************ FIRMWARE ************\n");
errorReport.append("SDK: ");
errorReport.append(Build.VERSION.SDK);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Release: ");
errorReport.append(Build.VERSION.RELEASE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Incremental: ");
errorReport.append(Build.VERSION.INCREMENTAL);
errorReport.append(LINE_SEPARATOR);
//after this you can do whatever you want , like i start an activity and show error log there
Intent i = new Intent(context,ExceptionActivity.class);
i.putExtra("exception",errorReport.toString());
context.startActivity(i);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
和添加此行
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
在每一个活动(其中未捕获的异常可以提高)
in every activity(where uncaught exception can raise)
这篇关于捕获错误和用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文