本文实例讲述了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,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开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity *** 作技巧总结》、《Android *** 作SQLite数据库技巧总结》、《Android *** 作json格式数据技巧总结》、《Android数据库 *** 作技巧总结》、《Android文件 *** 作技巧汇总》、《Android编程开发之SD卡 *** 作方法汇总》、《Android资源 *** 作技巧汇总》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android实现捕获未知异常并提交给服务器的方法全部内容,希望文章能够帮你解决Android实现捕获未知异常并提交给服务器的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)