ApplicationErrorReport简介和使用其实现Error Report功能

来源:互联网 发布:linux图形界面卡死 编辑:程序博客网 时间:2024/06/05 20:22

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:

            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 reportcandidate = “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.flagsreport.systemApp = (mServiceItem.mServiceInfo.applicationInfo.flags                        & ApplicationInfo.FLAG_SYSTEM) != 0;ApplicationErrorReport.RunningServiceInfo info                        = new ApplicationErrorReport.RunningServiceInfo();// info信息收集代码省略report.runningServiceInfo = info;  //这里发送IntentIntent 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,从中拿到对应的信息进行保存、存储、发送至对应的服务器

 //获取reportIntent localIntent = getIntent();ApplicationErrorReport report = ((ApplicationErrorReport)localIntent.getParcelableExtra("android.intent.extra.BUG_REPORT"));//接着就是从report中获取对应的息进行保存、存储、发送至对应的服务器

参照以上,即可以实现一个Error Report的基本功能。



0 0