问题
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