android异常处理发送消息给服务器
?
转自:http://www.cnblogs.com/stay/archive/2011/07/21/2113167.html
程序员最头疼的事情就是bug和debug。这次debug长达20天,搞的我心力交瘁。累,因为Android兼容性,不同手机会有不同的bug出来,而且很难复现,所以就上网找了下类似保存错误log到文件再上传到服务器,现把源码也共享出来。上传至服务器的代码我没加。相信大家都有现成的代码了。
先讲下原理,跟JavaEE的自定义异常捕获一样,将错误一直向上抛,然后在最上层统一处理。这里就可以获得Exception Message,进行保存操作
异常捕获类如下:
//??????????????? 可以只创建一个文件,以后全部往里面append然后发送,这样就会有重复的信息,个人不推荐
????????????????
String fileName =
"crash-"
+ System.currentTimeMillis()? +
".log"
;??
????????????????
File file =
new
File(Environment.getExternalStorageDirectory(), fileName);
????????????????
try
{
????????????????????
FileOutputStream fos =
new
FileOutputStream(file,
true
);
????????????????????
fos.write(message.getBytes());
????????????????????
for
(
int
i =
0
; i < stack.length; i++) {
????????????????????????
fos.write(stack[i].toString().getBytes());
????????????????????
}
????????????????????
fos.flush();
????????????????????
fos.close();
????????????????
}
catch
(Exception e) {
????????????????
}
????????????????
Looper.loop();??
????????????
}??
???
?????????
}.start();??
????????
return
false
;??
????
}??
???
?????
// TODO 使用HTTP Post 发送错误报告到服务器? 这里不再赘述
//??? private void postReport(File file) {??
//????? 在上传的时候还可以将该app的version,该手机的机型等信息一并发送的服务器,
//????? Android的兼容性众所周知,所以可能错误不是每个手机都会报错,还是有针对性的去debug比较好
//??? }??
}
??在Application onCreate时就注册ExceptionHandler,此后只要程序在抛异常后就能捕获到。
view sourceprint?public
class
App
extends
Application{
????????
@Override
??????????
public
void
onCreate() {??
????????????
super
.onCreate();??
????????????
CrashHandler crashHandler = CrashHandler.getInstance();??
????????????
//注册crashHandler??
????????????
crashHandler.init(getApplicationContext());??
????????
}??
}
view sourceprint?public
class
LogActivity
extends
Activity {
????
@Override
????
public
void
onCreate(Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.main);
????????
try
{
//制造bug
????????????
File file =
new
File(Environment.getExternalStorageState() ,
"crash.bin"
);
????????????
FileInputStream fis =
new
FileInputStream(file);
????????????
byte
[] buffer =
new
byte
[
1024
];
????????????
fis.read(buffer);
????????
}
catch
(Exception e) {
????????????
//这里不能再向上抛异常,如果想要将log信息保存起来,则抛出runtime异常,
//????????? 让自定义的handler来捕获,统一将文件保存起来上传
????????????
throw
new
RuntimeException(e);
????????
}
????
}
}
注意,如果catch后不throw就默认是自己处理了,ExceptionHandler不会捕获异常了。
再分享一个Log的封装类,只要在这里设置DEBUG的值就能让控制台是否打印出log
view sourceprint?public
class
DebugUtil {
????
public
static
final
String TAG =
"ICON"
;
????
public
static
final
boolean
DEBUG =
true
;
?????
?????
public
static
void
toast(Context context,String content){
????????
Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
????
}
?????
?????
public
static
void
debug(String tag,String msg){
????????
if
(DEBUG) {
????????????
Log.d(tag, msg);
????????
}
????
}
?????
?????
public
static
void
debug(String msg){
????????
if
(DEBUG) {
????????????
Log.d(TAG, msg);
????????
}
????
}
?????
?????
public
static
void
error(String tag,String error){
????????
Log.e(tag, error);
????
}
?????
?????
public
static
void
error(String error){
????????
Log.e(TAG, error);
????
}
}