multithreading - In Android i want to run countdown timer who can run in background also -
hi friends developing 1 app in requirement run timer in background popular candy crush game actual requirement when open app first time in day want start countdown timer 24:00:00 after time suppose leave application , open other application during same time countdown timer must running never pause or never stop
i visit 8,9 tutorial not getting exact result please me out kind of class may use or tutorial link please in advance
you have 2 options: service
, storing time of app launching. service
killed system @ time, have save time on ondestroy
method , when system relaunch service
system call oncreate
method need restore timer. second method easier. store current time on starting app , check differences. sample:
@override protected void onresume() { super.onresume(); final sharedpreferences appprefs = getsharedpreferences("appprefs", mode_private); final long launchtime = appprefs.getlong("launchtime", 0); final long currenttime = system.currenttimemillis(); if(launchtime == 0){ //first launch appprefs.edit().putlong("launchtime", currenttime); }else{ long diff = currenttime - launchtime; long minutespassed = diff / 1000 / 60; if(24 * 60 <= minutespassed){ //24 hours passed since app launching }else{ //24 hours didn't passed since app launching } } }
Comments
Post a Comment