Android dialog not showing when background task is running -
this extension of question here, can see of code both fragment containing button , service being used: android sending messages between fragment , service
i have button, when clicked start service, collect sensor data, insert database. when clicked again, want display progress dialog, wait existing background tasks finish , dismiss progress dialog.
i can correctly send messages , forth between fragment , service using handler
in fragment (as can seen in other question). problem progress dialog doesn't show in ui thread until after executor task queue has been cleared
in fragment, button onclick
looks this:
//show stop dialog stopdialog = new progressdialog(mainactivity); stopdialog.setmessage("stopping..."); stopdialog.settitle("saving data"); stopdialog.setprogressnumberformat(null); stopdialog.setcancelable(false); stopdialog.setmax(100); stopdialog.show(); log.d(tag, "dialog up"); //stop service mainactivity.stopservice(new intent(mainactivity, sensorservice.class));
and service ondestroy
looks this:
public void ondestroy() { //prevent new tasks being added thread executor.shutdown(); log.d(tag, "executor shutdown called"); try { //wait tasks finish before proceed while (!executor.awaittermination(1, timeunit.seconds)) { log.i(tag, "waiting current tasks finish"); } } catch (interruptedexception e) { executor.shutdownnow(); thread.currentthread().interrupt(); } if (executor.isterminated()){ //stop else once task queue clear unregisterreceiver(receiver); unregisterlistener(); wakelock.release(); dbhelper.close(); stopforeground(true); //dismiss progress dialog - commented out debugging //sendmessage("hide"); } }
expectation
when stop button clicked, progress dialog should show, ondestroy
gets called in service, executor queue finishes existing tasks, , dialog gets dismissed via message handler (this last part has been commented out can figure out going on)
reality
when stop clicked, in logcat can see message dialog up
, followed executor shutdown called
. while finishing current task queue can see waiting current tasks finish
. order of events correct, progress dialog doesn't display until after ondestroy
code, though (in fragment) tell show before service told stop.
i see lot of skipped frames after click stop button, presumably while executor trying clear queue: skipped 336 frames! application may doing work on main thread.
though i'm not sure why executor working in background thread causing ui not update
whats going on? ui blocking?
in mind dialog should show, , service told stop, work through rest of executor task queue, , dismissed. reason dialog show isn't happening until after executor terminates.
is there in code thats blocking ui thread showing progress dialog? have thought because executor running in background thread ui thread free show/display things dialogs
note: know can solve using asynctask
, , theres similar problem/solution here, want use executor this. questions deal activities , asynctasks, question uses strange combination of fragments, services , executors, cannot find questions/answers
edit:
after googling found question here, suggests awaittermination
might blocking ui thread. took of ondestroy
code waits executor queue clear , moved new thread:
public void ondestroy() { //prevent new tasks being added thread executor.shutdown(); log.d(tag, "executor shutdown called"); new thread(new runnable() { public void run() { try { //wait tasks finish before proceed while (!executor.awaittermination(1, timeunit.seconds)) { log.i(tag, "waiting current tasks finish"); } } catch (interruptedexception e) { executor.shutdownnow(); thread.currentthread().interrupt(); } if (executor.isterminated()) { //stop else once task queue clear unregisterreceiver(receiver); unregisterlistener(); wakelock.release(); dbhelper.close(); stopforeground(true); //dismiss progress dialog sendmessage("hide"); } } }).start(); }
now when hit stop, seems progress dialog shows instantly, exception every time:
exception dispatching input event. jni detected error in application: jni callobjectmethod called pending exception java.util.concurrent.rejectedexecutionexception: task com.example.app.sensorservice$inserthandler@35e04d3 rejected java.util.concurrent.threadpoolexecutor@2b28810[shutting down, pool size = 1, active threads = 1, queued tasks = 481, completed tasks = 2069]
(that first part of exception - long)
i'm not sure happening. while new thread starting, executor thread running execute
finish tasks in current queue. causing problem in how 2 threads working concurrently?
you have correctly identified reason dialog
not showing because ui thread blocked in awaittermination
.
the reason rejectedexecutionexception
submitting tasks execution after threadpoolexecutor
in shutting down state.
note executor enters shutdown call executor.shutdown()
. however, don't unregister listener sensormanager
until executor drained of pending tasks. during time receiving onsensorchanged
callbacks , calling executor.execute(...)
, each of result in exception.
the simple workaround check if executor.isshutdown()
before calling execute(...)
, more appropriate unregister listener sensormanager
before shutting down executor:
public class sensorservice extends service implements sensoreventlistener { // .... public void ondestroy() { // prevent new tasks being created. unregisterlistener(); // shutdown executor.shutdown(); // ... } }
Comments
Post a Comment