发布于 2017-03-25 23:18:47 | 137 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现捕获未知异常并提交给服务器的方法,涉及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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务