ANR 是android开发中常见的异常,报错的原因多种多样,还需要根据具体的现象找原因。
通常最常见的是在处理耗时的任务或者连接时间过长会导致ANR
1.?
10-27 09:08:37.031 ? 648 ?1118 E ActivityManager: ?+0% 924/kworker/2:0: 0% user + 0% kernel 10-27 09:08:37.031 ? 648 ?1118 E ActivityManager: ?+0% 1014/logcat: 0% user + 0% kernel 10-27 09:08:37.031 ? 648 ?1118 E ActivityManager: 80% TOTAL: 5.6% user + 31% kernel + 24% iowait + 18% softirq 10-27 09:08:37.031 ? 648 ?1118 E ActivityManager: CPU usage from 350044ms to 350044ms ago (1970-01-01 03:30:00.000 to 1970-01-01 03:30:00.000) with 0% awake: 10-27 09:08:37.031 ? 648 ?1118 E ActivityManager: 0% TOTAL: 0% user + 0% kernel 10-27 09:08:37.056 ? 648 ?1118 I ActivityManager: Killing 918:com.ync.menu/1000 (adj -800): bg anr 10-27 09:08:37.057 ? 648 ?1118 D ActivityManager: Completed ANR of com.ync.menu in 2145ms, latency 139ms 10-27 09:08:37.061 ? 648 ?1119 I DropBoxManagerService: add tag=system_app_anr isTagEnabled=true flags=0x2 10-27 09:08:37.179 ? 648 ?1002 I ActivityManager: Process com.ync.menu (pid 918) has died: pers PER 10-27 09:08:37.181 ? 648 ?1002 W ActivityManager: Scheduling restart of crashed service com.ync.menu/.system.CpServicein 0ms for persistent 10-27 09:08:37.182 ? 304 ? 304 I Zygote ?: Process 918 exited due to signal 9 (Killed) 10-27 09:08:37.183 ? 648 ? 743 I libprocessgroup: Successfully killed process cgroup uid 1000 pid 918 in 124ms 10-27 09:08:37.183 ? 648 ? 743 I libprocessgroup: Successfully killed process cgroup uid 1000 pid 918 in 0ms 10-27 09:08:37.192 ? 648 ?1002 W ActivityManager: Re-adding persistent process ProcessRecord{f4c15d9 918:com.ync.menu/1000} 10-27 09:08:37.200 ? 648 ?1002 D CompatibilityChangeReporter: Compat change id reported: 135634846; UID 1000; state: DISABLED 10-27 09:08:37.221 ? 304 ? 304 D Zygote ?: Forked child process 1120 10-27 09:08:37.225 ? 648 ? 735 I ActivityManager: Start proc 1120:com.ync.menu/1000 for restart com.ync.menu 10-27 09:08:37.548 ?1120 ?1120 I Zygote ?: seccomp disabled by setenforce 0 ?
由于在耗时任务在主线程中执行导致ANR
将任务放到子线程中处理
?? ? ? ?new Thread(new Runnable() { ? ? ? ? ? ? ?? ?@Override ? ? ? ? ? ? ?? ?public void run() {
? ? ? ? ? ? ? ? //...............耗时任务---------
? ? ?} ? ?}).start();
2.或者在主线程进行循环处理耗时任务时刷新UI这样也是会报ANR,我们也可以给个子线程让任务单独处理,主线程可以处理其他事情,等子线程任务处理完成后在去处理其他事情,这样可以避免报ANR 异常。
|