最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)捕獲未知異常并提交給服務(wù)器的方法

 更新時(shí)間:2016年08月17日 10:21:35   作者:llyofdream  
這篇文章主要介紹了Android實(shí)現(xiàn)捕獲未知異常并提交給服務(wù)器的方法,涉及Android的異常與錯(cuò)誤處理機(jī)制相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android實(shí)現(xiàn)捕獲未知異常并提交給服務(wù)器的方法。分享給大家供大家參考,具體如下:

在Android應(yīng)用中,即便應(yīng)用已經(jīng)投放市場(chǎng),但有時(shí)也會(huì)遇到一些未知的異常,此時(shí)如果能夠獲得用戶的反饋信息,那么對(duì)于我們應(yīng)用的開(kāi)發(fā)是一個(gè)很好的幫助

為了實(shí)現(xiàn)這樣的效果,我們需要做如下工作

寫一個(gè)類實(shí)現(xiàn)UncaughtExceptionHandler接口,重寫uncaughtException方法

功能描述:當(dāng)應(yīng)用出現(xiàn)了未知異常,應(yīng)用強(qiáng)制退出,應(yīng)用再次啟動(dòng)時(shí),提示用戶是否將錯(cuò)誤信息反饋給開(kāi)發(fā)者

public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
  private static final String TAG = "MyUncaughtExceptionHandler";
  // 將錯(cuò)誤信息保存到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;
  // 保存錯(cuò)誤原因的字段
  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();
    }
  }
  // 當(dāng)異常發(fā)生時(shí),會(huì)調(diào)用這個(gè)方法
  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();
  }
  // 下次開(kāi)啟應(yīng)用的時(shí)候,如果上次產(chǎn)生了未知異常則顯示對(duì)話框應(yīng)用與用戶反饋
  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到服務(wù)器
          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();
  }
}

在需要捕捉異常的地方調(diào)用

@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);
  }

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

郁南县| 泸西县| 额济纳旗| 徐州市| 易门县| 洞口县| 昭通市| 合水县| 饶河县| 汉寿县| 象州县| 高清| 大同市| 湖州市| 五莲县| 黔西县| 南川市| 西昌市| 五家渠市| 东光县| 泾川县| 紫金县| 高雄市| 洱源县| 柯坪县| 合江县| 嘉义市| 广安市| 淮滨县| 淮安市| 洛浦县| 烟台市| 广德县| 邢台市| 平南县| 嘉禾县| 密云县| 灵武市| 越西县| 静乐县| 洛南县|