从PackageManager获取应用列表后点击应用触发UI冻结的问题求助
从PackageManager获取应用列表后点击应用触发UI冻结的问题求助
大家好,我最近在开发一个Android应用,功能是通过PackageManager获取设备上所有已安装的系统应用和用户应用,然后用Jetpack Compose的LazyColumn搭配Card组件把这些应用展示在界面上。核心需求是用户点击任意一个Card时,由ViewModel来跟踪选中的应用,直到用户确认自己的选择。
但现在遇到了一个棘手的问题:每当我选中第二个应用,或者取消选中之前已经选中的应用时,整个APP就会直接冻结,随后Logcat会输出大量相关日志。我的测试设备是三星Galaxy Note9,刷的是iodeOS 4.23(基于Android 13)。
以下是问题发生时的Logcat日志片段:
2025-08-15 14:43:04.690 21721-21721 SelectAppsViewModel com.example.appinfotester E Selected app: AppInfo(label=Brave, packageName=com.brave.browser) 2025-08-15 14:43:04.690 21721-21721 SelectAppsViewModel com.example.appinfotester E Current count: [AppInfo(label=Brave, packageName=com.brave.browser)] 2025-08-15 14:43:22.994 21721-21721 SelectAppsViewModel com.example.appinfotester E Selected app: AppInfo(label=Discord, packageName=com.discord) 2025-08-15 14:43:22.994 21721-21721 SelectAppsViewModel com.example.appinfotester E Current count: [AppInfo(label=Brave, packageName=com.brave.browser), AppInfo(label=Discord, packageName=com.discord)] 2025-08-15 14:43:24.722 21721-21758 OpenGLRenderer com.example.appinfotester I Davey! duration=1705ms; Flags=0, FrameTimelineVsyncId=137348124, IntendedVsync=205704950715283, Vsync=205704950715283, InputEventId=92854293, HandleInputStart=205704951143444, AnimationStart=205704951146905, PerformTraversalsStart=205706634503058, DrawStart=205706634608519, FrameDeadline=205704967381949, FrameInterval=205704951133136, FrameStartTime=16730205, SyncQueued=205706640309212, SyncStart=205706640371327, IssueDrawCommandsStart=205706648072635, SwapBuffers=205706652782173, FrameCompleted=205706656262635, DequeueBufferDuration=20269, QueueBufferDuration=198115, GpuCompleted=205706656262635, SwapBuffersCompleted=205706654275212, DisplayPresentTime=205701507169292, CommandSubmissionCompleted=205706652782173, 2025-08-15 14:43:27.132 21721-21736 OpenGLRenderer com.example.appinfotester I Davey! duration=2428ms; Flags=0, FrameTimelineVsyncId=137348265, IntendedVsync=205706635577804, Vsync=205706652244470, InputEventId=0, HandleInputStart=205706655168866, AnimationStart=205706655170866, PerformTraversalsStart=205709043922172, DrawStart=205709044032749, FrameDeadline=205706673862859, FrameInterval=205706655158635, FrameStartTime=16709061, SyncQueued=205709056304018, SyncStart=205709056508325, IssueDrawCommandsStart=205709060475095, SwapBuffers=205709061317287, FrameCompleted=205709063887364, DequeueBufferDuration=18807, QueueBufferDuration=180500, GpuCompleted=205709063887364, SwapBuffersCompleted=205709061787595, DisplayPresentTime=205707071139481, CommandSubmissionCompleted=205709061317287, 2025-08-15 14:43:30.545 21721-21731 e.appinfotester com.example.appinfotester I NativeAlloc concurrent copying GC freed 12141(511KB) AllocSpace objects, 0(0B) LOS objects, 79% free, 6171KB/30MB, paused 126us,81us total 115.597ms 2025-08-15 14:43:31.277 21721-21737 OpenGLRenderer com.example.appinfotester I Davey! duration=1784ms; Flags=0, FrameTimelineVsyncId=137350734, IntendedVsync=205711433430105, Vsync=205711433430105, InputEventId=0, HandleInputStart=205711433927747, AnimationStart=205711433930439, PerformTraversalsStart=205713193868438, DrawStart=205713193951053, FrameDeadline=205711999260138, FrameInterval=205711433919862, FrameStartTime=16711093, SyncQueued=205713195881130, SyncStart=205713195949938, IssueDrawCommandsStart=205713209776207, SwapBuffers=205713216270092, FrameCompleted=205713217911322, DequeueBufferDuration=45000, QueueBufferDuration=166500, GpuCompleted=205713217911322, SwapBuffersCompleted=205713216808246, DisplayPresentTime=205710012114863, CommandSubmissionCompleted=205713216270092, 2025-08-15 14:43:32.928 21721-21737 OpenGLRenderer com.example.appinfotester I Davey! duration=1656ms; Flags=0, FrameTimelineVsyncId=137351077, IntendedVsync=205713204315733, Vsync=205713204315733, InputEventId=0, HandleInputStart=205713213289630, AnimationStart=205713213291015, PerformTraversalsStart=205714848140167, DrawStart=205714848227514, FrameDeadline=205713238183722, FrameInterval=205713213284169, FrameStartTime=16705327, SyncQueued=205714850149129, SyncStart=205714850216975, IssueDrawCommandsStart=205714855743552, SwapBuffers=205714858393398, FrameCompleted=205714860531783, DequeueBufferDuration=50230, QueueBufferDuration=589346, GpuCompleted=205714860531783, SwapBuffersCompleted=205714860248091, DisplayPresentTime=205710028669517, CommandSubmissionCompleted=205714858393398, 2025-08-15 14:43:34.534 21721-21737 OpenGLRenderer com.example.appinfotester I Davey! duration=1605ms; Flags=0, FrameTimelineVsyncId=137351159, IntendedVsync=205714857786844, Vsync=205714857786844, InputEventId=0, HandleInputStart=205714861618783, AnimationStart=205714861625860, PerformTraversalsStart=205716454996051, DrawStart=205716455072897, FrameDeadline=205714891515099, FrameInterval=205714
我自己观察到:ViewModel确实能正常记录选中的应用(日志里Brave和Discord的选中记录都正确输出),但之后就开始出现OpenGLRenderer的Davey!日志——这些都是帧耗时严重超标的提示(比如duration达到1700ms以上,远超过正常的16ms帧耗时),同时还有GC回收的日志,说明APP可能在大量占用内存或者频繁触发UI重组,导致UI线程被完全阻塞。
我初步怀疑的几个方向:
- 是不是ViewModel里用了可变集合(比如
MutableList)存储选中应用,更新时没有正确触发Compose的重组通知? - 是不是
LazyColumn的Item没有设置稳定的key,导致每次选中状态变化时整个列表都被强制重组? - 会不会是获取应用信息时的某些操作被意外重复执行,导致UI线程负载过高?
有没有大佬遇到过类似的问题,或者能给我一些排查的方向?非常感谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

