ANR:Application Not Responding

本文深入探讨了Android应用程序中ANR(Application Not Responding)对话框产生的原因及其解决策略。重点阐述了如何合理设计应用程序响应性能,避免主线程阻塞,确保后台操作在子线程中进行,以及正确处理Intent广播接收器以防止ANR。此外,文章还提供了关键的性能优化技巧,帮助开发者构建更加流畅、响应迅速的应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自:360百科

在Android上,如果你的应用程序一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框。

用户可以选择“等待”而让程序继续运行,也可以选择“强制关闭”。所以一个流畅的合理的应用程序中不能出现anr,而让用户每次都要处理这个对话框。因此,在程序里对响应性能的设计很重要,这样系统不会显示ANR给用户。默认情况下,在android中Activity的最长执行时间是5秒,BroadcastReceiver的最长执行时间则是10秒。

考虑上面的ANR定义,让我们来研究一下为什么它会在Android应用程序里发生和如何最佳构建应用程序来避免ANR。

  Android应用程序通常是运行在一个单独的线程(例如,main)里。这意味着你的应用程序所做的事情如果在主线程里占用了太长的时间的话,就会引发ANR对话框,因为你的应用程序并没有给自己机会来处理输入事件或者Intent广播。

  因此,运行在主线程里的任何方法都尽可能少做事情。特别是,Activity应该在它的关键生命周期方法(如onCreate()和onResume())里尽可能少的去做创建操作。潜在的耗时操作,例如网络或数据库操作,或者高耗时的计算如改变位图尺寸,应该在子线程里(或者以数据库操作为例,通过异步请求的方式)来完成。然而,不是说你的主线程阻塞在那里等待子线程的完成——也不是调用Thread.wait()或是Thread.sleep()。替代的方法是,主线程应该为子线程提供一个Handler,以便完成时能够提交给主线程。以这种方式设计你的应用程序,将能保证你的主线程保持对输入的响应性并能避免由于5秒输入事件的超时引发的ANR对话框。这种做法应该在其它显示UI的线程里效仿,因为它们都受相同的超时影响。

  IntentReceiver执行时间的特殊限制意味着它应该做:在后台里做小的、琐碎的工作如保存设定或者注册一个Notification。和在主线程里调用的其它方法一样,应用程序应该避免在BroadcastReceiver里做耗时的操作或计算。但不再是在子线程里做这些任务(因为BroadcastReceiver的生命周期短),替代的是,如果响应Intent广播需要执行一个耗时的动作的话,应用程序应该启动一个Service。顺便提及一句,你也应该避免在Intent Receiver里启动一个Activity,因为它会创建一个新的画面,并从当前用户正在运行的程序上抢夺焦点。如果你的应用程序在响应Intent广播时需要向用户展示什么,你应该使用Notification Manager来实现。

  一般来说,在应用程序里,100到200ms是用户能感知阻滞的时间阈值。因此,这里有一些额外的技巧来避免ANR,并有助于让你的应用程序看起来有响应性。如果你的应用程序为响应用户输入正在后台工作的话,可以显示工作的进度(ProgressBar和ProgressDialog对这种情况来说很有用)。特别是游戏,在子线程里做移动的计算。如果你的应用程序有一个耗时的初始化过程的话,考虑可以显示一个Splash Screen或者快速显示主画面并异步来填充这些信息。在这两种情况下,你都应该显示正在进行的进度,以免用户认为应用程序被冻结了。

D:\Users\Downloads\Log\youtube\bugreport-RMX5313IN-AP3A.240905.015.A2-2025-06-19-11-34-32\bugreport-RMX5313IN-AP3A.240905.015.A2-2025-06-19-11-34-32.txt (155 hits) Line 15125: WINDOW MANAGER LAST ANR (dumpsys window lastanr) Line 116973: ------ VM TRACES AT LAST ANR (/data/anr/anr_2025-06-19-11-19-10-783: 2025-06-19 11:19:18) ------ Line 116973: ------ VM TRACES AT LAST ANR (/data/anr/anr_2025-06-19-11-19-10-783: 2025-06-19 11:19:18) ------ Line 116986: anr < Line 116996: anr < Line 117006: anr < Line 117016: anr < Line 199851: "anrV33-1" prio=5 tid=16 TimedWaiting Line 201580: at anrp.run(PG:35) Line 201676: at anrp.run(PG:35) Line 201731: at anrp.run(PG:35) Line 201752: at anrp.run(PG:35) Line 221720: ------ ANR FILES (ls -lt /data/anr/) ------ Line 221722: -rw------- 1 system system 4661511 2025-06-19 11:19 anr_2025-06-19-11-19-10-783 Line 221723: -rw------- 1 system system 5125123 2025-06-19 11:18 anr_2025-06-19-11-18-28-676 Line 221724: -rw------- 1 system system 5110113 2025-06-19 11:18 anr_2025-06-19-11-17-59-943 Line 221725: -rw------- 1 system system 5170843 2025-06-19 11:15 anr_2025-06-19-11-15-30-180 Line 221726: -rw------- 1 system system 5211710 2025-06-19 11:15 anr_2025-06-19-11-14-59-431 Line 221727: -rw------- 1 system system 5249604 2025-06-19 11:13 anr_2025-06-19-11-13-33-120 Line 221728: -rw------- 1 system system 2550071 2025-06-18 12:01 anr_2025-06-18-12-01-29-071 Line 221729: -rw------- 1 system system 2661757 2025-06-18 12:01 temp_anr_204762592841438826.txt Line 221730: -rw------- 1 system system 4501038 2025-06-18 12:01 anr_2025-06-18-12-00-54-648 Line 221731: -rw------- 1 system system 2790621 2025-06-18 12:00 temp_anr_6131620412329269920.txt Line 221732: -rw------- 1 system system 4774064 2025-06-18 12:00 anr_2025-06-18-12-00-30-005 Line 221733: -rw------- 1 system system 2962603 2025-06-18 12:00 temp_anr_4436968846206332345.txt Line 221734: -rw------- 1 system system 4909259 2025-06-18 12:00 anr_2025-06-18-12-00-06-676 Line 221735: -rw------- 1 system system 5561152 2025-06-18 11:59 anr_2025-06-18-11-59-15-525 Line 221736: -rw------- 1 system system 2486347 2025-06-18 11:58 anr_2025-06-18-11-58-50-107 Line 221737: -rw------- 1 system system 2513502 2025-06-18 11:58 temp_anr_7627520979123128223.txt Line 221738: -rw------- 1 system system 2547664 2025-06-18 11:58 temp_anr_7306873162887901303.txt Line 221739: -rw------- 1 system system 4468182 2025-06-18 11:58 anr_2025-06-18-11-58-19-450 Line 221740: -rw------- 1 system system 5457289 2025-06-18 11:56 anr_2025-06-18-11-56-37-891 Line 221741: -rw------- 1 system system 1325229 2025-06-18 11:54 anr_2025-06-18-11-53-47-163 Line 221742: -rw------- 1 system system 5504454 2025-06-18 11:52 anr_2025-06-18-11-52-01-409 Line 239615: backstage_power/android.app.gate_fgs_timeout_anr_behavior: READ_ONLY + DISABLED (system), DISABLED (device_config) Line 240829: system_performance/com.android.server.utils.anr_timer_freezer: READ_ONLY + DISABLED (system), DISABLED (device_config) Line 240830: system_performance/com.android.server.utils.anr_timer_service: READ_ONLY + DISABLED (system), DISABLED (device_config) Line 259422: service_start_foreground_anr_delay_ms=10000 Line 259437: short_fgs_anr_extra_wait_duration=10000 Line 288694: ACTIVITY MANAGER LAST ANR (dumpsys activity lastanr) Line 300808: importance=400 pss=0.00 rss=0.00 description=bg anr: Input dispatching timed out (Application does not have a focused window). state=empty trace=/data/system/procexitstore/anr_2025-06-18-11-53-47-163.gz Line 300808: importance=400 pss=0.00 rss=0.00 description=bg anr: Input dispatching timed out (Application does not have a focused window). state=empty trace=/data/system/procexitstore/anr_2025-06-18-11-53-47-163.gz Line 307584: importance=230 pss=0.00 rss=347MB description=user request after error: No response to onStartJob state=empty trace=/data/system/procexitstore/anr_2025-06-19-11-19-10-783.gz Line 307588: importance=230 pss=0.00 rss=483MB description=remove task state=empty trace=/data/system/procexitstore/anr_2025-06-19-11-17-59-943.gz Line 307604: importance=230 pss=0.00 rss=452MB description=user request after error: Input dispatching timed out (8b6aa1e com.google.android.youtube/com.google.android.youtube.app.honeycomb.Shell$HomeActivity (server) is not responding. Waited 5001ms for FocusEvent(hasFocus=true)). state=empty trace=/data/system/procexitstore/anr_2025-06-19-11-15-30-180.gz Line 307608: importance=100 pss=0.00 rss=0.00 description=user request after error: Input dispatching timed out (96080e6 com.google.android.youtube/com.google.android.youtube.app.honeycomb.Shell$HomeActivity (server) is not responding. Waited 5000ms for FocusEvent(hasFocus=true)). state=empty trace=/data/system/procexitstore/anr_2025-06-19-11-14-59-431.gz Line 437707: Job Completions com.coloros.gallery3d/com.oplus.gallery.framework.abilities.scan.manager.GalleryScanService: successful_finish(7x) anr(1x) Line 437719: 11 starts, 1 anrs Line 439978: Job Completions @androidx.work.systemjobscheduler@com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobService: unknown:-1(1x) canceled(14x) successful_finish(24x) anr(1x) Line 439989: 15 starts, 6 anrs Line 441158: Job Completions in.mohalla.sharechat/androidx.work.impl.background.systemjob.SystemJobService: canceled(5x) successful_finish(6x) anr(9x) Line 441159: Job Completions in.mohalla.sharechat/com.google.android.datatransport.runtime.scheduling.jobscheduling.JobInfoSchedulerService: successful_finish(3x) anr(6x) Line 473593: backstage_power/android.app.gate_fgs_timeout_anr_behavior=false Line 475571: system_performance/com.android.server.utils.anr_timer_freezer=false Line 475572: system_performance/com.android.server.utils.anr_timer_service=false Line 476935: 2025-06-17 11:36:45 system_app_anr (compressed text, 232873 bytes) Line 476936: 2025-06-17 11:37:09 system_app_anr (compressed text, 201571 bytes) Line 476937: 2025-06-17 11:40:22 system_app_anr (compressed text, 222123 bytes) Line 476938: 2025-06-17 11:41:13 system_app_anr (compressed text, 185557 bytes) Line 476939: 2025-06-17 11:41:38 system_app_anr (compressed text, 241609 bytes) Line 476940: 2025-06-17 11:42:13 system_app_anr (compressed text, 248915 bytes) Line 476941: 2025-06-17 11:54:53 system_app_anr (compressed text, 167694 bytes) Line 476942: 2025-06-17 11:55:43 system_app_anr (compressed text, 190039 bytes) Line 476943: 2025-06-17 11:58:39 system_app_anr (compressed text, 245539 bytes) Line 476944: 2025-06-17 12:17:04 system_app_anr (compressed text, 214445 bytes) Line 476945: 2025-06-17 12:22:01 system_app_anr (compressed text, 241492 bytes) Line 476947: 2025-06-18 11:52:20 system_app_anr (compressed text, 260400 bytes) Line 476948: 2025-06-18 11:54:13 system_app_anr (compressed text, 196046 bytes) Line 476949: 2025-06-18 11:57:01 system_app_anr (compressed text, 245540 bytes) Line 476950: 2025-06-18 11:58:36 system_app_anr (compressed text, 256724 bytes) Line 476951: 2025-06-18 11:58:53 system_app_anr (compressed text, 83697 bytes) Line 476952: 2025-06-18 11:59:29 system_app_anr (compressed text, 255379 bytes) Line 476953: 2025-06-18 12:00:19 system_app_anr (compressed text, 248849 bytes) Line 476959: 2025-06-19 11:13:43 system_app_anr (compressed text, 238808 bytes) Line 476960: 2025-06-19 11:15:08 system_app_anr (compressed text, 236177 bytes) Line 476961: 2025-06-19 11:15:37 system_app_anr (compressed text, 220894 bytes) Line 476962: 2025-06-19 11:18:08 system_app_anr (compressed text, 223886 bytes) Line 476963: 2025-06-19 11:18:36 system_app_anr (compressed text, 218263 bytes) Line 476964: 2025-06-19 11:19:19 system_app_anr (compressed text, 236600 bytes) Line 483662: es_u_anr_count=3 Line 483663: es_u_anr_window_ms=21600000 Line 483780: <0>com.google.android.youtube::anr: Line 483792: <0>in.mohalla.sharechat::anr: Line 483836: <0>com.google.android.apps.subscriptions.red::anr: Line 483844: <0>com.google.android.youtube::anr: Line 483852: <0>com.google.android.googlequicksearchbox::anr: Line 483858: <0>com.android.providers.calendar::anr: Line 483868: <0>com.android.providers.downloads::anr: Line 483876: <0>com.google.android.apps.messaging::anr: Line 483884: <0>com.oplus.sau::anr: Line 483892: <0>com.heytap.market::anr: Line 483900: <0>com.google.android.configupdater::anr: Line 483908: <0>com.google.android.providers.media.module::anr: Line 483916: <0>com.google.android.apps.safetyhub::anr: Line 483924: <0>in.mohalla.sharechat::anr: Line 483932: <0>com.android.vending::anr: Line 483942: <0>com.google.android.adservices.api::anr: Line 483950: <0>android::anr: Line 483958: <0>com.android.launcher3::anr: Line 483966: <0>com.instagram.android::anr: Line 483974: <0>com.google.android.deskclock::anr: Line 483982: <0>com.google.android.as::anr: Line 483990: <0>com.google.android.gm::anr: Line 484000: <0>com.google.android.apps.tachyon::anr: Line 484008: <0>com.google.android.permissioncontroller::anr: Line 484014: <0>com.google.android.setupwizard::anr: Line 484022: <0>com.android.providers.settings::anr: Line 484030: <0>com.facebook.services::anr: Line 484038: <0>com.google.android.as.oss::anr: Line 484046: <0>com.google.android.apps.wellbeing::anr: Line 484054: <0>com.google.android.dialer::anr: Line 484062: <0>com.google.android.apps.nbu.files::anr: Line 484070: <0>com.google.android.apps.docs::anr: Line 484078: <0>com.google.android.apps.maps::anr: Line 484086: <0>com.google.android.webview::anr: Line 484094: <0>com.coloros.weather2::anr: Line 484102: <0>com.google.android.networkstack::anr: Line 484110: <0>com.google.android.rkpdapp::anr: Line 484118: <0>com.google.android.contacts::anr: Line 484126: <0>com.android.chrome::anr: Line 484134: <0>com.nearme.gamecenter::anr: Line 484142: <0>com.nearme.statistics.rom::anr: Line 484150: <0>com.google.android.gms::anr: Line 484158: <0>com.google.android.tts::anr: Line 484166: <0>com.google.android.apps.walletnfcrel::anr: Line 484174: <0>com.google.android.partnersetup::anr: Line 484182: <0>com.google.android.videos::anr: Line 484190: <0>com.coloros.lockassistant::anr: Line 484200: <0>com.google.android.apps.photos::anr: Line 484208: <0>com.google.android.calendar::anr: Line 484218: <0>com.facebook.katana::anr: Line 484226: <0>com.coloros.phonemanager::anr: Line 484234: <0>com.android.imsserviceentitlement::anr: Line 484242: <0>com.android.settings::anr: Line 484250: <0>com.fitbit.FitbitMobile::anr: Line 484258: <0>com.grofers.customerapp::anr: Line 484266: <0>com.myntra.android::anr: Line 484274: <0>com.google.android.apps.turbo::anr: Line 484282: <0>com.oplus.lfeh::anr: Line 484288: <0>com.google.android.apps.carrier.carrierwifi::anr: Line 484298: <0>com.oppo.quicksearchbox::anr: Line 484306: <0>com.google.android.apps.youtube.music::anr: Line 484314: <0>com.facebook.appmanager::anr: Line 484322: <0>com.coloros.gallery3d::anr: Line 484330: <0>com.snapchat.android::anr: Line 484340: <0>com.google.android.inputmethod.latin::anr: Line 484348: <0>com.glance.internet::anr: Line 484354: <0>com.google.android.apps.restore::anr: Line 484363: Category{anr}: 3 events in +6h0m0s0ms Line 506990: 4x unknown:-1, 38x canceled, 82x successful_finish, 1x anr Line 544392: com.unisoc.traceur.anrstartdumpperfetto: Line 546673: com.unisoc.traceur.anrsavedumpperfetto: Line 708996: _id:585 name:anr_show_background pkg:com.android.settings value:0 default:0 defaultSystemSet:true Line 709073: _id:275 name:dropbox:data_app_anr pkg:com.google.android.gms value:disabled Line 709643: setting: anr_show_background generation:2
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值