二. 崩溃日志保存到远程服务器
实现途径 :
-- 1. 自己开发服务器端 : 在上面的保存文件的基础上, 在开发一个服务器端, 使用网络编程接口将打印到文件的内容上传到服务器端;
-- 2. 使用第三方服务 : 使用 Testin 的崩溃大师, 地址 http://crash.testin.cn/app?scnavbar ;
1. 集成崩溃大师步骤
(1) 创建应用 获取 key 值
创建应用步骤 :
-- 1. 创建界面 : http://crash.testin.cn/apm/task/create ;
-- 2. 填写一个应用信息 :
-- 3. 获取 key : 点击"提交并获取 Appkey" 按钮, 获取到了 AppKey "0da6263ca1f5b84a2dd405b07227f483";
(2) 设置类型
第二步设置类型, 选择默认的应用即可 :
(3) 下载 jar 包
jar 包简介 : 集成崩溃大师, 只需要集成一个 jar 包即可, 点击如下按钮即可下载 该 jar 包;
-- jar 包内容 : 下载后解压, jar 包名称是 testinagent.jar ;
(4) 导入 SDK
导入 SDK : 在应用下 创建一个 libs 目录, 将jar 包拷贝进去即可;
(5) 配置 AndroidManifest.xml 文件
在配置文件中添加如下用户权限即可 :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.GET_TASKS" />
(6) 初始化设置
在代码中注册 :
//初始化 Testin 崩溃大师 TestinAgent.init(getApplicationContext(), "0da6263ca1f5b84a2dd405b07227f483", "octopus");
2. 测试示例
(1) 日志信息
测试成功会打印如下信息 :
04-27 20:11:40.890: E/TestinAgent(16432): **************************************** 04-27 20:11:40.890: E/TestinAgent(16432): A Java crash caught by TestinAgent, pkg=cn.org.octpus.crash 04-27 20:11:40.890: E/TestinAgent(16432): ---------------------------------------- 04-27 20:11:40.890: E/TestinAgent(16432): reason: java.lang.IllegalStateException: Could not execute method of the activity 04-27 20:11:40.890: E/TestinAgent(16432): ---------------------------------------- 04-27 20:11:40.890: E/TestinAgent(16432): stacktrace: 04-27 20:11:40.890: E/TestinAgent(16432): at android.view.View$1.onClick(View.java:3804) 04-27 20:11:40.890: E/TestinAgent(16432): at android.view.View.performClick(View.java:4439) 04-27 20:11:40.890: E/TestinAgent(16432): at android.widget.Button.performClick(Button.java:142) 04-27 20:11:40.890: E/TestinAgent(16432): at android.view.View$PerformClick.run(View.java:18395) 04-27 20:11:40.890: E/TestinAgent(16432): at android.os.Handler.handleCallback(Handler.java:725) 04-27 20:11:40.890: E/TestinAgent(16432): at android.os.Handler.dispatchMessage(Handler.java:92) 04-27 20:11:40.890: E/TestinAgent(16432): at android.os.Looper.loop(Looper.java:176) 04-27 20:11:40.890: E/TestinAgent(16432): at android.app.ActivityThread.main(ActivityThread.java:5319) 04-27 20:11:40.890: E/TestinAgent(16432): at java.lang.reflect.Method.invokeNative(Native Method) 04-27 20:11:40.890: E/TestinAgent(16432): at java.lang.reflect.Method.invoke(Method.java:511) 04-27 20:11:40.890: E/TestinAgent(16432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 04-27 20:11:40.890: E/TestinAgent(16432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 04-27 20:11:40.890: E/TestinAgent(16432): at dalvik.system.NativeStart.main(Native Method) 04-27 20:11:40.890: E/TestinAgent(16432): Caused by: java.lang.reflect.InvocationTargetException 04-27 20:11:40.890: E/TestinAgent(16432): at java.lang.reflect.Method.invokeNative(Native Method) 04-27 20:11:40.890: E/TestinAgent(16432): at java.lang.reflect.Method.invoke(Method.java:511) 04-27 20:11:40.890: E/TestinAgent(16432): at android.view.View$1.onClick(View.java:3799) 04-27 20:11:40.890: E/TestinAgent(16432): ... 12 more 04-27 20:11:40.890: E/TestinAgent(16432): Caused by: java.lang.ArithmeticException: divide by zero 04-27 20:11:40.890: E/TestinAgent(16432): at cn.org.octpus.crash.MainActivity.onClick(MainActivity.java:20) 04-27 20:11:40.890: E/TestinAgent(16432): ... 15 more 04-27 20:11:40.890: E/TestinAgent(16432): ---------------------------------------- 04-27 20:11:40.890: E/TestinAgent(16432): deviceinfo: 04-27 20:11:40.890: E/TestinAgent(16432): Device: samsung/SM-G3812 04-27 20:11:40.890: E/TestinAgent(16432): OS: 4.2.2 04-27 20:11:40.890: E/TestinAgent(16432): AppVersion: 1.0 04-27 20:11:40.890: E/TestinAgent(16432): PackageName: cn.org.octpus.crash 04-27 20:11:40.890: E/TestinAgent(16432): Activity: MainActivity 04-27 20:11:40.890: E/TestinAgent(16432): Total Disk Space: 2328 MB ; Free Disk Space: 1688 MB 04-27 20:11:40.890: E/TestinAgent(16432): Total SD Space: 2308 MB ; Free SD Space: 1668 MB 04-27 20:11:40.890: E/TestinAgent(16432): CPU Usage: 17.742 % 04-27 20:11:40.890: E/TestinAgent(16432): Memory Usage: 12 MB 04-27 20:11:40.890: E/TestinAgent(16432): Userinfo: 04-27 20:11:40.890: E/TestinAgent(16432): ****************************************
(2) 后台信息
后台崩溃数据 :
-- 控制台信息 : 进入崩溃大师的控制台, 就会看到刚看到的 CrashInfoDemo 应用, 此时我们估计产生的异常已经打印出来了;
-- 点击进入该应用的详情 :
-- 详细的崩溃信息查看 :