源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现捕获未知异常并提交给服务器的方法

  • 时间:2020-09-22 02:03 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现捕获未知异常并提交给服务器的方法
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/848.htm]Android操作SQLite数据库技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/845.htm]Android操作json格式数据技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/224.htm]Android编程开发之SD卡操作方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部