APP層級處理未捕獲異常,APP層級處理捕獲

來源:互聯網
上載者:User

APP層級處理未捕獲異常,APP層級處理捕獲

前言:

  項目APP有時候會出現Crash,然後就是彈出系統強制退出的對話方塊,點擊關閉APP。

  有的APP進行了處理,會發現,當程式出現異常的時候,會Toast一個提示“程式出現異常,3秒後將退出程式”。3秒後即關閉程式而不再顯示強制關閉的對話方塊。

  那麼它們是如何處理沒有try-catch 捕獲到的異常 並 進行介面友好提示最佳化的處理呢。

  這裡我們通過一個demo學習一下。

 

----------------------------------------------------------------------------------------------------------------

一、建立一個類 CrashHandler 實現 UncaughtExceptionHandler 介面 , 當程式發生未捕獲異常時 由該類進行處理

  

public class CrashHandler implements Thread.UncaughtExceptionHandler{    private Thread.UncaughtExceptionHandler mDefaultHandler;    Context context;    //CrashHandler執行個體    private static CrashHandler instance;    /** 擷取CrashHandler執行個體 ,單例模式 */    public static CrashHandler getInstance() {        if (instance == null)            instance = new CrashHandler();        return instance;    }    /**     * 初始化     */    public void init(Context context) {        this.context = context;        //擷取系統預設的UncaughtException處理器        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();        //設定該CrashHandler為程式的預設處理器        Thread.setDefaultUncaughtExceptionHandler(this);    }    
  
  //實現uncaughException方法 @Override public void uncaughtException(Thread thread, Throwable ex) { if (!solveException(ex) && mDefaultHandler != null){ //如果使用者沒有處理則讓系統預設的異常處理器處理 mDefaultHandler.uncaughtException(thread, ex); }else{
       // 等待2秒鐘後關閉程式 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } /* * 錯誤處理 * */ private boolean solveException(Throwable e){ if (e == null){ return false; } new Thread() { @Override public void run() { Looper.prepare(); Toast.makeText(context,"程式出現異常,2秒後退出",Toast.LENGTH_SHORT).show(); Looper.loop(); } }.start(); return true; }}

 

二、建立一個基礎Application的類MApplication

在onCreate()方法中進行初始化

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

別忘了在AndroidManifest.xml中添加

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:name=".MApplication"        android:theme="@style/AppTheme">

 

三、類比一個異常

  給一個沒有綁定的TextView賦值  , null 指標的異常 

public class MainActivity extends Activity {    private TextView text;    private TextView aa;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text  = (TextView) findViewById(R.id.text);        text.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                text.setText("ni hao");                aa.setText("nihao ");            }        });    }}

 

 

因為我們不可能給每一個方法都try-catch。所以總會有沒有捕獲到的異常出現。

進行對未捕獲異常的處理,可以提高一個使用者體驗。

開發人員們 也可以 在這個處理中添加異常分析,將出現的異常裝置、原因、時間等資訊提交到自己的伺服器上方便以後分析。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.