日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)編程開發(fā)Android → android 異常處理 對(duì)異常進(jìn)行全局捕捉

      android 異常處理 對(duì)異常進(jìn)行全局捕捉

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來源:西西整理時(shí)間:2012/9/5 16:24:40字體大。A-A+

      作者:佚名點(diǎn)擊:145次評(píng)論:0次標(biāo)簽: 異常處理

      最近新產(chǎn)品測(cè)試,頻頻出現(xiàn)異常。所以需要對(duì)異常進(jìn)行全局捕捉。
      翻閱大量帖子、源碼終于找到了UncaughtExceptionHandler接口。廢話不多說還是直接上源碼吧。


      首先實(shí)現(xiàn)UncaughtExceptionHandler

      public class CatchHandler implements UncaughtExceptionHandler{

              private CatchHandler() {
              }

              public static CatchHandler getInstance() {

                      return mCatchHandler;
              }

              private static CatchHandler mCatchHandler = new CatchHandler();

              private Context mContext;

              private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

              @Override
              public void uncaughtException(Thread thread, Throwable ex) {
                      if (thread.getName().equals("main")) {
                              ToastException(thread, ex);
                              try {
                                      Thread.sleep(3000);
                              } catch (InterruptedException e) {
                              }
                              android.os.Process.killProcess(android.os.Process.myPid());
                              System.exit(1);
                      } else {
                              handleException(thread, ex);
                      }

              }

              public void init(Context context) {
                      mContext = context;
                      Thread.setDefaultUncaughtExceptionHandler(this);
              }

              private void ToastException(final Thread thread, final Throwable ex) {
                      new Thread() {
                              @Override
                              public void run() {
                                      Looper.prepare();
                                      StringBuilder builder = new StringBuilder();
                                      builder.append("At thread: ").append(thread.getName())
                                                      .append("\n");
                                      builder.append("Exception is :\n").append(ex.getMessage());

                                      Toast.makeText(mContext, builder.toString(), Toast.LENGTH_LONG)
                                                      .show();
                                      Looper.loop();
                              }
                      }.start();
              }

              private void handleException(final Thread thread, final Throwable ex) {
                      Intent intent =new Intent("per.xch.test2_35.main");
                      <font color="#00ed00">intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</font>
                      mContext.startActivity(intent);
              }
      }

       

      然后在項(xiàng)目中引用

      public class CatchApplication extends Application {
              @Override
              public void onCreate() {
                      super.onCreate();
                      CatchHandler.getInstance().init(getApplicationContext());
              }
      }

      注意

      <application
              <font color="#00ed00">android:name=".CatchApplication"</font>

      最后測(cè)試下

      public class MainActivity extends Activity {

          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              
              new Thread(new Runnable() {
                              
                              @Override
                              public void run() {
                                       throw new NullPointerException("please catch me! sub thread");
                              }
                      }).start();
              throw new NullPointerException("please catch me! sub thread");
             
          }

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              getMenuInflater().inflate(R.menu.activity_main, menu);
              return true;
          }
      }

      之所以在主線程和其他線程采用不同處理方式因?yàn)橹骶程崩潰很大程度上就沒的救了。
      還有要注意的是intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      原因很簡(jiǎn)單,源碼寫的很明白


      ContextImpl.java

        @Override
          public void startActivity(Intent intent) {
              if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
                  throw new AndroidRuntimeException(
                          "Calling startActivity() from outside of an Activity "
                          + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
                          + " Is this really what you want?");
              }
              mMainThread.getInstrumentation().execStartActivity(
                  getOuterContext(), mMainThread.getApplicationThread(), null,
                  (Activity)null, intent, -1);
          }

      至于怎么把a(bǔ)ctivity怎么變成dialog我就不廢話嘍,吼,閃人。

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)