Need to implement timer (with nature of variable time intervals) in an android application -


my edited code.here interval vector stores delays required , index initialized 0.i got mistake.this have been doing,

public class mainactivity{ private int index;        //some code , initializations      public void starttimer{      index=0;     private timer timer=new timer();     private timertask timertask=new mytimertask();    // timer.schedule(timertask,0,1000);         //this line causing trouble      }      private class mytimertask extends timertask         {                 public void run() {                  handler.postdelayed(new runnable() {          int limit = interval.size();          @override         public void run() {             if (index < limit) {                 integer seconddelay = interval.get(k);                 log.e(tag, "index= " + index + " interval= " + seconddelay + " seconds");                  //some code                  long delay = seconddelay * 1000;                 index++;                 handler.postdelayed(this, delay);             } else {                 handler.removecallbacksandmessages(null); //cancelling handler.postdelayed                 log.e(tag, "cancelling timer");                 timer.cancel();             }         }     }, 0);     } } } 

the log output after every 1 second.

the commented line causing error.it should timer.schedule(timertask,0); , working correctly.

i had similar issue , solved handler

final handler handler = new handler();         handler.postdelayed(new runnable() {             int minute = 0;             @override             public void run() {                 {                     log.d("tag", "next run after "+minute+" minutes");                     long delay = minute * 60000;                     minute++;                     handler.postdelayed(this,delay);                 }             }         }, 0); 

and logcat

01-27 11:49:55.830 23761-23761/? d/tag: next run after 0 minutes 01-27 11:49:55.945 23761-23761/? d/tag: next run after 1 minutes 01-27 11:50:56.015 23761-23761/? d/tag: next run after 2 minutes 01-27 11:52:56.115 23761-23761/? d/tag: next run after 3 minutes 01-27 11:55:56.225 23761-23761/? d/tag: next run after 4 minutes ...and on 

my example has minute interval can adapt seconds.

hope helps.


edit modified example based on comments

final arraylist<integer> interval = new arraylist<>(); interval.add(1); interval.add(5); interval.add(10); interval.add(3);  final handler handler = new handler(); handler.postdelayed(new runnable() {     int index = 0;      @override     public void run() {         {              if (index < interval.size()) {                 integer seconddelay = interval.get(index);                  log.d("tag", "next run after " + seconddelay + " seconds");                 long delay = seconddelay * 1000;                 index++;                 handler.postdelayed(this, delay);             } else {                 log.d("tag", "i finish");             }          }     } }, 0); 

logcat:

01-27 17:51:09.940 28113-28113/? d/tag: next run after 1 seconds 01-27 17:51:10.940 28113-28113/? d/tag: next run after 5 seconds 01-27 17:51:15.950 28113-28113/? d/tag: next run after 10 seconds 01-27 17:51:25.960 28113-28113/? d/tag: next run after 3 seconds 01-27 17:51:28.965 28113-28113/? d/tag: finish 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -