I need a timer for my program. I have written it and it works fine on PC in emulalator program (Android 1.5/2.2). But it doesn't work on the real device (Android 1.5). What am I doing wrong?
TimerTask task = new TimerTask() {
public void run() {
if (condition) {
myFunc();
} else {
this.cancel();
}
}
};
Timer timer = new Timer();
timer.schedule(task, 500, 85);
You need to cancel() timer not the timer task.
Related
I using pushy notification in my driver android app.So my problem is that when app get notification(passenger detail),I want to execute Asyntask from Braodcast receiver after 30 seconds. I have used code given below -
final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new Runnable() {
public void run() {
String cancel = preferences.getString("cancel_status", "1");
Log.d("cancell", "cancell");
if (cancel != "1")
new ConfirmRejectService(context).execute(message, "2");
}
}, 0, 5, TimeUnit.SECONDS);
You should try handler to solve your problem. It have worked for me.`
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{}
I have two services which run periodically based on condition. In once condition the services are run with 5 second timer while in other they run with 10 mins timer.
But when I want to switch from 5 second to 10 mins timer, it does not happen instantly while it runs 4 times extra. I am canceling timer pefectly but does not seems like working..here is code
public final static Handler serviceHandler = new Handler(){
public void handleMessage(Message msg){
if(null != serviceTimer) {
Log.d(TAG, "Coming insisisi");
serviceTimer.cancel();
serviceTimer.purge();
serviceTimer = null;
}
serviceTimer = new Timer();
serviceTimer.scheduleAtFixedRate(new serviceMonitorAcc(service, target), DELAY_TIMER_TIME, TIMER_START_TIME);
serviceTimer.scheduleAtFixedRate(new serviceMonitorTrafficStats(service, target), DELAY_TIMER_TIME, TIMER_START_TIME);
Log.d(TAG, "TIMER STATTAA"+serviceTimer);
}
};
based on conditions I set the values of variable DELAY_TIMER_TIME and TIMER_START_TIME
private void setTimerInfo(String check){
if(check != null) {
if (check.equals("enable")) {
DELAY_TIMER_TIME = 0;
TIMER_START_TIME = 5000;
} else {
DELAY_TIMER_TIME = 60000;
TIMER_START_TIME = 600000;
}
}
}
the time setting working time only issue is it does not get killed instanlty and start new serviceTimer with new timer setting.
I also tried to kill timertask serviceMonitorAcc and serviceMonitorTrafficStats separatly, but did not work ...any suggestion or pointer for it...
is it possible to use a timerTask,like:
timer = new Timer();
task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
new AsyncTask().execute();
}
});
}
};
timer.schedule(task, 0, 4000);
I need to start a periodically task when activity is on background ,instead of using a Service, when activity is onPause() and to prevent to be killed by the system?
thanks in advance
You can start your TimerTask and inside it AsyncTast this way. They will run as long as your application process is running, but they will not prevent killing of your Activity and Application. When considering which activity or process to kill android does not take into account threads or timers running inside your app, only Activities, Services, BroadcastReceivers etc count.
I am looking for a way to send data from my timer task to service. I have seen many other post related to handlers and all but I do not want to run in my main thread
you can write a singleton class which will hold data which you want to share. This is just a way not standard.
TimerTask task = new TimerTask() {
public void run() {
if (condition) {
MySingleton.getInstance().setData(put data here);
} else {
timer.cancel();
}
}
};
Timer timer = new Timer();
timer.schedule(task, 1000, 1000);
//then cancel timer somewhere by
timer.cancel();
then in service you can get data by some thing like this
DataType myData = MySingleton.getInstance().getData();
I have an android application that gets user location every 2 minutes using requestLocationUpdates service, so now once it reaches 25 minutes I want the running service to be stopped. Is it possible?
Thank you.
Make a timer (1500000 = 25 minutes in milliseconds):
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//stop your service here
}
}, 0, 1500000);