• android捕获全局异常,并对异常做出处理


    在做项目时,经常会把错误利用异常抛出去,这样在开发时就可以通过手机抛的异常排查错误,很方便。但是当程序开发完毕,版本稳定,需要上线时,为了避免抛出异常影响用户感受,可以捕获全局异常,对异常做出处理。

    具体的实方法如下:

    利用Thread.UncaughtExceptionHandler 获取异常,并对异常做出处理:

    public class MyUncaughtExceptionHandler implements 
            Thread.UncaughtExceptionHandler { 
        private Thread.UncaughtExceptionHandler a; 
        MyUncaughtExceptionHandler(){ 
             this.a = Thread.getDefaultUncaughtExceptionHandler(); 
        } 
        @Override 
        public void uncaughtException(Thread thread, Throwable ex) { 
            Log.i("huilurry","ppppppppppppp="+ex.getMessage()); 
        //是否抛出异常 
    //        if(a!=null) 
    //        a.uncaughtException(thread, ex); 
        } 
    }

    具体调用:

    public class HuiLurryActivty extends Activity { 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            String t=android.provider.Settings.System.getString(getContentResolver(), "android_id"); 
            Log.i("huilurry","android_id="+t); 
            huilurry(); 
            throw new NullPointerException("is null"); 
        } 
        HandlerThread localHandlerThread; 
        Handler handler; 
        private void huilurry() 
        { 
           localHandlerThread=new HandlerThread("huilurry"); 
            localHandlerThread.start(); 
            handler=new Handler(localHandlerThread.getLooper()); 
            Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); 
        } 
    }

    主要是利用了Hander和HandlerThread。

    源代码见:http://wangjun-memory.googlecode.com/svn/trunk/android.huilurry

  • 相关阅读:
    CentOS FTP基于虚拟用户的配置
    CentOS6.5 FTP配置
    MAC 隐藏文件的显示
    nignx知识点总结
    什么是函数式编程
    javascript有用代码片段
    PHP百杂
    好文:javascript中的四种循环
    好文:node.js最佳实践
    好文:不使用匿名函数的三个理由
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168212.html
Copyright © 2020-2023  润新知