Question

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?

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top