当前位置:操作系统 > 安卓/Android >>

Android借助Application重写App的Crash(完整版)

MainActivity如下:

[java]
package cn.testcrash; 
import android.app.Activity; 
import android.os.Bundle; 
/**
 * Demo描述:
 * 借助于Application自定义Crash
 * 
 * 备注说明:
 * 1 在获取Crash信息后可依据业务上传服务器或者保存至SDCard
 * 2 在uncaughtException()中的第三步——退出程序.此处只是做了
 *   最简单的处理.完备的操作请参见:
 *   http://blog.csdn.net/lfdfhl/article/details/9261147
 *   http://blog.csdn.net/lfdfhl/article/details/9260605
 *   
 * 参考资料:
 * 1 http://blog.csdn.net/xiaanming/article/details/9344703
 * 2 http://blog.csdn.net/itachi85/article/details/9102021
 * Thank you very much
 */ 
public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    //Crash  
    private void init(){ 
        System.out.println((9727/0)+""); 
    } 

package cn.testcrash;
import android.app.Activity;
import android.os.Bundle;
/**
 * Demo描述:
 * 借助于Application自定义Crash
 *
 * 备注说明:
 * 1 在获取Crash信息后可依据业务上传服务器或者保存至SDCard
 * 2 在uncaughtException()中的第三步——退出程序.此处只是做了
 *   最简单的处理.完备的操作请参见:
 *   http://blog.csdn.net/lfdfhl/article/details/9261147
 *   http://blog.csdn.net/lfdfhl/article/details/9260605
 *  
 * 参考资料:
 * 1 http://blog.csdn.net/xiaanming/article/details/9344703
 * 2 http://blog.csdn.net/itachi85/article/details/9102021
 * Thank you very much
 */
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }
 //Crash
 private void init(){
  System.out.println((9727/0)+"");
 }
}

CrashApplication如下:

[java]
package cn.testcrash; 
import android.app.Application; 
 
public class CrashApplication extends Application { 
    @Override 
    public void onCreate() { 
        super.onCreate(); 
        CrashHandler crashHandler=CrashHandler.getInstance(); 
        //指定Crash时的处理程序  
        crashHandler.setCrashHandler(getApplicationContext()); 
    } 

package cn.testcrash;
import android.app.Application;

public class CrashApplication extends Application {
 @Override
 public void onCreate() {
  super.onCreate();
  CrashHandler crashHandler=CrashHandler.getInstance();
  //指定Crash时的处理程序
  crashHandler.setCrashHandler(getApplicationContext());
 }
}

CrashHandler如下:

[java]
package cn.testcrash; 
import java.lang.Thread.UncaughtExceptionHandler; 
import android.content.Context; 
import android.os.Looper; 
import android.widget.Toast; 
public class CrashHandler implements UncaughtExceptionHandler { 
    private Context mContext; 
    private static CrashHandler mCrashHandler=new CrashHandler(); 
     
    public static CrashHandler getInstance(){ 
        return mCrashHandler; 
    } 
     
    /**
     * 设置当线程由于未捕获到异常而突然终止的默认处理程序。
     */ 
    public void setCrashHandler(Context context){ 
        mContext=context; 
        Thread.setDefaultUncaughtExceptionHandler(this); 
    } 
     
    /**
     * 当发生Crash时调用该方法
     */ 
    @Override 
    public void uncaughtException(Thread thread, Throwable throwable) { 
         //1 保存错误日志到SD卡  
         Utils.saveCrashInfoToSDCard(mContext, throwable); 
         //2 提示Crash信息  
         showCrashTipToast(); 
         try { 
            Thread.sleep(3000); 
        } catch (Exception e) { 
        } 
         //3 退出应用  
         System.exit(0); 
    } 
     
    private void showCrashTipToast() { 
        new Thread(new Runnable() { 
            @Override 
            public void run() { 
                Looper.prepare(); 
                Toast.makeText(mContext, "I am very sorry", Toast.LENGTH_LONG).show(); 
                Looper.loop(); 
            } 
        }).start(); 
    } 
 

package cn.testcrash;
import java.lang.Thread.UncaughtExceptionHandler;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;
public class CrashHandler implements UncaughtExceptionHandler {
 private Context mContext;
 private static CrashHandler mCrashHandler=new CrashHandler();
 
 public static CrashHandler getInstance(){
  return mCrashHandler;
 }
 
 /**
  * 设置当线程由于未捕获到异常而突然终止的默认处理程序。
  */
 public void setCrashHandler(Context context){
 &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,