ANR调试过程分析

本文详细介绍了ANR(Application Not Responding)的三种类型:Key Dispatching timeout、Broadcast timeout和服务执行超时,并分析了导致ANR的原因,如主线程阻塞、广播接收超时、服务执行超时。针对这些问题,提出了使用独立线程、Handler等解决方案,同时分享了ANR调试的标准步骤,包括查看logcat、分析堆栈信息和定位真实原因。文章列举了几个具体的ANR案例,如AsyncTask中的ContentResolver操作、网络数据读写阻塞UI线程以及内存泄漏导致的ANR问题。
摘要由CSDN通过智能技术生成

1 anr 类型

ANR: Application Not Responding

Include 3 types:

1.1 Key Dispatching timeout ANR5s)

The input event dont responding in specified time which is defined in ActivityManagerService.java such as 5 second and then ANR happened. At this time the main thread must been blocked, maybe it try to do a network access such as check update or send http request in ui thread, or do database operation or do some work like image convert.

How to solve the problem? Maybe you need a dependent thread to do these time consuming work or use handler to do connection work between UI thread and other threads. And avoided the use sleep(), wait() api or synchronized methods in main thread.

1.2 Broadcast timeout ANR10s)

The main thread cant execute the OnReceive function on specified time such as 10s for foreground application and 20s for background application.

1.3 Service execution timeout ANR(20s)

In this case, the anr happened means that the main thread cant execute the OnCreate or On StartCommand of Service finish in 20s

 

2 Main thread

include:

1). Activity:onCreate(), onResume(), onDestroy(), onKeyDown(), onClick(),etc

2). AsyncTask: onPreExecute(), onProgressUpdate(), onPostExecute(), onCancel,etc

3). Mainthread handler: handleMessage(), post*(runnable r), etc

4.) other

3 Debug SOP

I always follow the steps to debug ANR issue.

1) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值