android 全局异常的处理 详解
Android游戏《歪把子炮》源码
http://www.eoeandroid.com/thread-197425-1-1.html
Android 新版捕鱼达人源码
http://www.eoeandroid.com/thread-197437-1-1.html
Android版本的手机RSS阅读器的源码
http://www.eoeandroid.com/thread-197465-1-1.html
最近新产品测试,频频出现异常。所以需要对异常进行全局捕捉。
翻阅大量帖子、源码终于找到了UncaughtExceptionHandler接口。废话不多说还是直接上源码吧。
首先实现UncaughtExceptionHandler
public class CatchHandler implements UncaughtExceptionHandler{ private CatchHandler() {} public static CatchHandler getInstance() { retu mCatchHandler;} private static CatchHandler mCatchHandler = new CatchHandler(); private Context mContext; private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); @Overridepublic 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() {@Overridepublic 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);}}
然后在项目中引用
public class CatchApplication extends Application {@Overridepublic void onCreate() {super.onCreate();CatchHandler.getInstance().init(getApplicationContext());}}
注意
<application<font color="#00ed00">android:name=".CatchApplication"</font>
最后测试下
public class MainActivity extends Activity { @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); new Thread(new Runnable() { @Overridepublic void run() { throw new NullPointerException("please catch me! sub thread");}}).start();throw new NullPointerException("please catch me! sub thread");} @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);retu true;}}
之所以在主线程和其他线程采用不同处理方式因为主线程崩溃很大程度上就没的救了。
还有要注意的是intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
原因很简单,源码写的很明白
ContextImpl.java
@Overridepublic 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);}
至于怎么把activity怎么变成dialog我就不废话喽,吼,闪人。
原文链接:http://www.eoeandroid.com/thread-197442-1-1.html
来源链接:https://www.cnblogs.com/vus520/archive/2012/09/05/2671669.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。