Android实现捕获未知异常并提交给服务器的方法
作者:llyofdream 时间:2022-07-15 23:49:29
本文实例讲述了Android实现捕获未知异常并提交给服务器的方法。分享给大家供大家参考,具体如下:
在Android应用中,即便应用已经投放市场,但有时也会遇到一些未知的异常,此时如果能够获得用户的反馈信息,那么对于我们应用的开发是一个很好的帮助
为了实现这样的效果,我们需要做如下工作
写一个类实现UncaughtExceptionHandler接口,重写uncaughtException方法
功能描述:当应用出现了未知异常,应用强制退出,应用再次启动时,提示用户是否将错误信息反馈给开发者
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
private static final String TAG = "MyUncaughtExceptionHandler";
// 将错误信息保存到sharepreference中
private static SharedPreferences bugPreferences;
private static SharedPreferences.Editor bugEditor;
private static Context mContext;
private static PackageInfo packageInfo;
private UncaughtExceptionHandler defaultUncaughtExceptionHandler;
private static HandleProgressDialog progressDialog;
// 保存错误原因的字段
private static String bugExistStr = "";
private static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
}
};
public MyUncaughtExceptionHandler(Context context) {
try {
mContext = context;
packageInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
bugPreferences = context.getSharedPreferences("bug", 0);
bugEditor = bugPreferences.edit();
defaultUncaughtExceptionHandler = Thread
.getDefaultUncaughtExceptionHandler();
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// 当异常发生时,会调用这个方法
public void uncaughtException(Thread th, Throwable t) {
try {
// 保存bug
saveBugText(t);
defaultUncaughtExceptionHandler.uncaughtException(th, t);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveBugText(Throwable ex) throws FileNotFoundException {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
bugEditor.putString("bugText", writer.toString());
bugEditor.commit();
}
// 下次开启应用的时候,如果上次产生了未知异常则显示对话框应用与用户反馈
public static void showBugReportDialog(final Context context) {
bugExistStr = context.getSharedPreferences("bug", 0).getString(
"bugText", "");
if (bugExistStr != null && !bugExistStr.equals("")) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.bug_report);
builder.setMessage(R.string.whether_report_to_developer);
builder.setNegativeButton(R.string.cancel, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(dialog);
}
});
builder.setPositiveButton(R.string.send, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 提交bug到服务器
postBugReportInBackground(context);
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
private static void postBugReportInBackground(final Context context) {
progressDialog = new HandleProgressDialog(context);
progressDialog.show();
new Thread(new Runnable() {
public void run() {
postBugReport();
// 将之前的bug信息清除掉
if (bugExistStr != null) {
bugEditor.putString("bugText", "");
bugEditor.commit();
}
handler.sendEmptyMessage(0);
}
}).start();
}
/**
* Send Bug Report.
*/
private static void postBugReport() {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("device", Build.DEVICE));
nvps.add(new BasicNameValuePair("model", Build.MODEL));
nvps.add(new BasicNameValuePair("sdk-version", Build.VERSION.SDK));
nvps.add(new BasicNameValuePair("apk-version", packageInfo.versionName));
nvps.add(new BasicNameValuePair("bug", bugExistStr));
try {
HttpPost httpPost = new HttpPost(Constants.BaseUrl
+ "c=main&a=androidCrash");
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void finish(DialogInterface dialog) {
if (bugExistStr != null) {
bugEditor.putString("bugText", "");
bugEditor.commit();
}
dialog.dismiss();
}
}
在需要捕捉异常的地方调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appApplication = (APPApplication) getApplication();
appApplication.activites.add(this);
initViews();
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(
MainActivity.this));
MyUncaughtExceptionHandler.showBugReportDialog(this);
}
希望本文所述对大家Android程序设计有所帮助。
标签:Android,捕获异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Spring与SpringMVC父子容器的关系与初始化
2023-02-08 12:09:05
![](https://img.aspxhome.com/file/2023/3/79273_0s.jpg)
Android中ListView如何分页加载数据
2023-09-08 18:44:35
![](https://img.aspxhome.com/file/2023/5/137355_0s.jpg)
详解StackExchange.Redis通用封装类分享
2022-07-06 00:41:32
![](https://img.aspxhome.com/file/2023/3/68643_0s.png)
Java二维数组实现数字拼图效果
2021-11-21 20:39:17
移动端WebApp隐藏地址栏的方法
2022-09-26 20:26:29
Android开发高仿课程表的布局实例详解
2022-02-01 18:33:19
![](https://img.aspxhome.com/file/2023/1/138861_0s.gif)
VsCode搭建Java开发环境的方法
2023-06-17 13:29:19
![](https://img.aspxhome.com/file/2023/9/130919_0s.png)
Java ArrayList实现班级信息管理系统
2023-09-17 15:15:25
![](https://img.aspxhome.com/file/2023/7/85897_0s.jpg)
如何把spring boot项目部署到tomcat容器中
2023-10-08 18:53:51
SpringCloud分布式链路追踪组件Sleuth配置详解
2023-11-28 23:58:42
![](https://img.aspxhome.com/file/2023/6/60666_0s.png)
C#实现简易计算器功能(附源码)
2021-07-18 00:16:00
![](https://img.aspxhome.com/file/2023/9/108659_0s.jpg)
C#实现控制线程池最大数并发线程
2023-02-23 14:50:43
![](https://img.aspxhome.com/file/2023/5/119595_0s.png)
深入理解java中Arrays.sort()的用法
2021-05-26 22:25:58
一文搞懂Spring中的注解与反射
2023-11-11 11:38:18
![](https://img.aspxhome.com/file/2023/8/58758_0s.jpg)
Java Pattern与Matcher字符串匹配案例详解
2022-03-25 16:07:19
![](https://img.aspxhome.com/file/2023/7/102627_0s.png)
java mail使用qq邮箱发邮件的配置方法
2023-07-02 07:58:56
C#实现抓取和分析网页类实例
2023-09-21 04:27:32
C#给PDF文件添加水印
2023-11-10 17:08:36
使用JMX监控Zookeeper状态Java API
2023-05-14 02:27:26
Android 使用ViewPager实现左右循环滑动及轮播效果
2022-10-28 15:23:21