ApplicationErrorReport简介和使用其实现Error Report功能
2014-07-29 21:20
260 查看
Android4.0(SDK Level 14)版本添加了ApplicationErrorReport接口,此接口用来处理当应用出现异常时抓取报告错误信息。有了此接口,对应用开发者,可以利用它捕获自身应用异常,保存信息,使用网络反馈给自己,及时处理异常,为客户提供新的APK,以提高客户体验;针对手机系统开发者,可以利用此接口捕获系统应用的信息,并通过客户反馈,或者利用网络等手段收集信息,便于及时处理系统应用异常。
一、下面就针对此接口进行介绍:
ApplicationErrorReport,描述一个应用错误。
此report有一个类型,类型为一下五种之一:
TYPE_NONE:未初始化的错误报告,
TYPE_CRASH:关于应用程序崩溃的错误报告,有关崩溃信息储存在crashInfo中,
TYPE_ANR:有关无响应的应用程序的错误报告,有关ANR的信息存储在anrInfo中
TYPE_BATTERY:有关的消耗太多的电池应用程序的错误报告,有关电池使用的信息存储在batteryInfo中,
TYPE_RUNNING_SERVICE:有关应用程序留下不必要的service运行的报告,有关service电池使用的信息存储在runningServiceInfo。
ApplicationErrorReport中有四个嵌套类:
ApplicationErrorReport.AnrInfo:描述无响应错误的应用程序,
ApplicationErrorReport.BatteryInfo:描述了电池的使用报告,
ApplicationErrorReport.CrashInfo:描述一个应用程序崩溃,
ApplicationErrorReport.RunningServiceInfo:描述了一个正在运的Service。
其他的信息可以详见官网的API。
下面看此接口的使用:
而在android系统在已经对此接口已经使用,且已经留接口给开发者:例如设置——》应用——》正在运行——》(点击某一个应用,进入)正在运行的应用,就可以看到如下界面:
如上图中红圈中的“报告”,这里为什么其为灰色,先看代码RunnigServiceDetail.java:
1. 打开report功能,这里看到要detail.mReportButton不灰显的判断条件:
a.需要SEND_ACTION_APP_ERROR此功能打开,即如下:
b.需要有ErrorRoportReceiver对象不为null,需要修改ApplicatioErrorReport.java中的方法:
2.发送report
3.接下来就是在对应的ErrorReprotReciver中获取ApplicatErrorReport实例report,从中拿到对应的信息进行保存、存储、发送至对应的服务器
参照以上,即可以实现一个Error Report的基本功能。
一、下面就针对此接口进行介绍:
ApplicationErrorReport,描述一个应用错误。
此report有一个类型,类型为一下五种之一:
TYPE_NONE:未初始化的错误报告,
TYPE_CRASH:关于应用程序崩溃的错误报告,有关崩溃信息储存在crashInfo中,
TYPE_ANR:有关无响应的应用程序的错误报告,有关ANR的信息存储在anrInfo中
TYPE_BATTERY:有关的消耗太多的电池应用程序的错误报告,有关电池使用的信息存储在batteryInfo中,
TYPE_RUNNING_SERVICE:有关应用程序留下不必要的service运行的报告,有关service电池使用的信息存储在runningServiceInfo。
ApplicationErrorReport中有四个嵌套类:
ApplicationErrorReport.AnrInfo:描述无响应错误的应用程序,
ApplicationErrorReport.BatteryInfo:描述了电池的使用报告,
ApplicationErrorReport.CrashInfo:描述一个应用程序崩溃,
ApplicationErrorReport.RunningServiceInfo:描述了一个正在运的Service。
其他的信息可以详见官网的API。
下面看此接口的使用:
而在android系统在已经对此接口已经使用,且已经留接口给开发者:例如设置——》应用——》正在运行——》(点击某一个应用,进入)正在运行的应用,就可以看到如下界面:
如上图中红圈中的“报告”,这里为什么其为灰色,先看代码RunnigServiceDetail.java:
int enabled = Settings.Global.getInt(getActivity().getContentResolver(), Settings.Global.SEND_ACTION_APP_ERROR, 0); if (enabled != 0 && si != null) { detail.mInstaller = ApplicationErrorReport.getErrorReportReceiver( getActivity(), si.mServiceInfo.packageName, si.mServiceInfo.applicationInfo.flags); detail.mReportButton.setEnabled(detail.mInstaller != null); } else { detail.mReportButton.setEnabled(false); }
1. 打开report功能,这里看到要detail.mReportButton不灰显的判断条件:
a.需要SEND_ACTION_APP_ERROR此功能打开,即如下:
Settings.Global.getInt(getActivity().getContentResolver(), Settings.Global.SEND_ACTION_APP_ERROR, 1); // 修改0为1
b.需要有ErrorRoportReceiver对象不为null,需要修改ApplicatioErrorReport.java中的方法:
public static ComponentName getErrorReportReceiver(Context context, String packageName, int appFlags) { // check if error reporting is enabled in secure settings int enabled = Settings.Global.getInt(context.getContentResolver(), Settings.Global.SEND_ACTION_APP_ERROR, 1);// 修改0为1 if (enabled == 0) { return null; } //…代码省略 // if the error app is on the system image, look for system apps // error receiver if ((appFlags&ApplicationInfo.FLAG_SYSTEM) != 0) { if (!packageName.matches("(com.google.android.).*|(com.android.vending)|(com.android.chrome)")) {//这里判读我们需要收集那些应用的error report candidate = “com.example.exceptionlogreport”;//这里设置//ErrorReportReceiver,canditate为包名 result = getErrorReportReceiver(pm, packageName, candidate); if (result != null) { return result; } } } //…代码省略 }
2.发送report
//这里封装report中的信息 ApplicationErrorReport report = new ApplicationErrorReport(); report.type = ApplicationErrorReport.TYPE_RUNNING_SERVICE; report.packageName = mServiceItem.mServiceInfo.packageName; report.installerPackageName = mInstaller.getPackageName(); report.processName = mServiceItem.mRunningService.process; report.time = System.currentTimeMillis(); report.systemApp = (mServiceItem.mServiceInfo.applicationInfo.flags report.systemApp = (mServiceItem.mServiceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; ApplicationErrorReport.RunningServiceInfo info = new ApplicationErrorReport.RunningServiceInfo(); // info信息收集代码省略 report.runningServiceInfo = info; //这里发送Intent Intent result = new Intent(Intent.ACTION_APP_ERROR); result.setComponent(mInstaller); result.putExtra(Intent.EXTRA_BUG_REPORT, report); result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(result);
3.接下来就是在对应的ErrorReprotReciver中获取ApplicatErrorReport实例report,从中拿到对应的信息进行保存、存储、发送至对应的服务器
//获取report Intent localIntent = getIntent(); ApplicationErrorReport report = ((ApplicationErrorReport)localIntent.getParcelableExtra("android.intent.extra.BUG_REPORT")); //接着就是从report中获取对应的息进行保存、存储、发送至对应的服务器
参照以上,即可以实现一个Error Report的基本功能。
相关文章推荐
- 使用ASP.NET的Application对象实现在线人数统计的功能
- ASP.NET中使用Application对象实现简单在线人数统计功能
- ASP.NET中使用Application对象实现简单在线人数统计功能
- Linux平台,使用JavaComm3 API及SMSLib项目实现在Web Application中发送手机短信的功能
- SpringCloud 微服务简介及搭建(本文使用Spring封装后的API,实现负载均衡的功能)
- CDC使用技巧之最快最方便的实现放大缩小功能
- 使用Spring framework实现定时器功能
- 使用Windows服务实现投票结果的自动发送功能
- C#实现SMTP服务器,使用TCP命令实现,功能比较完善
- linux下使用系统调用编程实现copy命令功能
- 关于使用LinkButton实现点击超链完成文件下载的功能
- 使用xml绑定实现分页功能
- 使用SHAREPOINT架构企业门户和实现OA功能
- 使用Updater Application Block实现自动更新例子
- SQL SERVER 2005 EXPRESS将在明年六月份实现REPORT SERVER功能
- 使用Application_Error事件处理程序把异常记录到系统事件日志
- 一个使用监听器模式实现的J2ME网络编程框架,包括一个简单的登录功能实现(含源代码)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- crystal report排序功能的实现
- 使用DATATGRID实现分组小计功能