ANR (Force close/Wait) while generating a big enough list

允我心安 提交于 2020-01-14 19:37:29

问题


I am creating a kind of file explorer, in which if any application files are copied to phone/SD card I am showing it on an activity with dialog theme to user. For every new "app.apk" copied I am appending the name of the app and location to the dialog and the list is scrollable. My problem is, if I copy some 200+ apps onto the storage at once the list population gives Force close/wait dialog (ANR). How can I prevent my app from getting this kind of ANR?


回答1:


If some large calculation and UI tries to run at the same time and it takes enough time to launch (approximately over 5 sec) then the system concerns user to close the application.

To solve this, an option is to distribute your operations into different child thread and then run UI on an asynchronous thread (runOnUiThread) for scheduling application task.

new Thread() {
    @Override
    public void run() {
        try {
            // code runs in a thread
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // code runs in a UI(main) thread
                }
            });
        } catch (final Exception ex) {
        }
    }
}.start();


来源:https://stackoverflow.com/questions/12855097/anr-force-close-wait-while-generating-a-big-enough-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!