java - ScheduledExecutorService as timeout shows no AlertDialog -


i'm trying implement timeout behaviour in app. there should warning (alertdialog) 5 seconds before timeout happens.

i want use scheduledexecutorservice so.

here relevant code far :

private final context context = this;  private scheduledexecutorservice sexservice = executors.newscheduledthreadpool(2);  private runnablescheduledfuture<?> sfuturetimeout; private runnablescheduledfuture<?> sfuturedisconnect;  private final runnable timeoutrunnable = new runnable(){     @override     public void run() {              showtimeoutalertdialog();     }    }; private final runnable disconnectrunnable = new runnable(){     @override     public void run() {         disconnect();        }    }; 

and methods handle timeout behaviour :

private void settimeout(){     sfuturetimeout = (runnablescheduledfuture<?>) sexservice.schedule(timeoutrunnable, 5, timeunit.seconds); } 

settimeout called in oncreate(), app should disconnect 5s after launch.

private void showtimeoutalertdialog(){      new alertdialog.builder(context)             .settitle("disconnect in 5s")             .setcancelable(false)             .setpositivebutton("abort",                     new dialoginterface.onclicklistener() {                         @override                         public void onclick(dialoginterface dialog, int id) {                             sfuturedisconnect.cancel(false);                             settimeout();                         }                     }).show();        sfuturedisconnect = (runnablescheduledfuture<?>) sexservice.schedule(disconnectrunnable, 5, timeunit.seconds); } 

here's problems facing :

  • if runnable called in "settimeout" set 'disconnectrunnable', works fine, app disconnects after 5s.

  • when set 'timeoutrunnable', alertdialog not shown + app never disconnects though 'disconnectrunnable' should called after 5s in "showtimeoutalertdialog"!?

i think scheduledexecutorservice went wrong here, can't find solution.

thank :)

you trying show alertdialog not ui thread never work method showtimeoutalertdialog() called worker thread created in scheduled thread pool. can use handler purpose:

public class myactivity extends activity {      private final context context = this;      private static handler mhandler = new handler();      private final runnable timeoutrunnable = new runnable(){         @override         public void run() {             showtimeoutalertdialog();         }     };     private final runnable disconnectrunnable = new runnable(){         @override         public void run() {             disconnect();         }     };      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         settimeout();     }      private void disconnect() {         log.e("myactivity", "disconnected");     }      private void settimeout(){         mhandler.postdelayed(timeoutrunnable, 5000);     }      private void showtimeoutalertdialog(){          new alertdialog.builder(context)                 .settitle("disconnect in 5s")                 .setcancelable(false)                 .setpositivebutton("abort",                         new dialoginterface.onclicklistener() {                             @override                             public void onclick(dialoginterface dialog, int id) {                                 mhandler.removecallbacks(disconnectrunnable);                                 settimeout();                             }                         }).show();          mhandler.postdelayed(disconnectrunnable, 5000);     } } 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -