App怎么做才能永不崩溃,安卓内存优化面试

本文探讨了Android应用程序如何防止因异常导致的崩溃,详细分析了进程退出和无响应的情况。介绍了Android系统中Handler、Looper和Message机制的重要性,以及如何自定义UncaughtExceptionHandler避免程序异常退出。此外,还分享了作者的个人经历和Android开发学习资源。
摘要由CSDN通过智能技术生成

if (RuntimeInit.DEBUG) {

Slog.d(RuntimeInit.TAG, “RuntimeInit: Starting application from zygote”);

}

Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, “ZygoteInit”);

RuntimeInit.redirectLogStreams();

RuntimeInit.commonInit();

ZygoteInit.nativeZygoteInit();

return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,

classLoader);

}

protected static final void commonInit() {

if (DEBUG) Slog.d(TAG, “Entered RuntimeInit!”);

/*

  • set handlers; these apply to all threads in the VM. Apps can replace

  • the default handler, but not the pre handler.

*/

LoggingHandler loggingHandler = new LoggingHandler();

RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler);

Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));

}

可以看到在ZygoteInit.zygoteInit()中已经设置了setDefaultUncaughtExceptionHandler(),而ZygoteInit是进程初始化的过程。 Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));

当程序出现异常会回调到KillApplicationHandler.uncaughtException(Thread t, Throwable e)

@Override

public void uncaughtException(Thread t, Throwable e) {

try {

ensureLogging(t, e);

// Don’t re-enter – avoid infinite loops if crash-reporting crashes.

if (mCrashing) return;

mCrashing = true;

// Try to end profiling. If a profiler is running at this point, and we kill the

// process (

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值