android stopservice after 25minutes - android

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);

Related

Cancelling the previous called task using timer

I am using Timer for calling a method after 5 sec. When i call the same method again before 5 sec the previous called method should get cancelled. How to do it?
Thanks
You need not to re-initiate again Timer with TimerTask again. You can schedule it like this.
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
If you need to cancel timer any time, you can use this.
timer.cancel(); // Terminates this timer, discarding any currently scheduled tasks.
timer.purge(); // Removes all cancelled tasks from this timer's task queue.

Multiple services with same interval of time in android

How to run multiple services in background with same interval of time in android? . I tried with AlarmManager but in this it is not running with same intervals like every 5 mins(Sometimes its running correctly but not all the times). Please suggest me best way to achieve this. Thanks in advance.
To run the multiple services in particular interval, you can use timertask
Timer timer =new Timer();
TimerTask timerTask= new TimerTask() {
public void run() {
//TODO
}
};
timer.schedule(timerTask, 1000, 1000);
Example for Service,
public class ServcieSample extends Servcie{
public void onCreate(){
}
}
Inside onCreate you can create any number of threads or asynctask for background operations.

Is there any workaround to make Android service run every 30 seconds (not every 1 minute)

I have made an android app which starts a service.
I want it to run every 30 seconds. Please don't worry about battery, it is not a public use app and it will run only ONCE for 3-4 hours in the entire week when the game is being played.
Now the problem I am facing is that initially when the app starts and service starts it runs every 30 seconds as expected but once the phone is locked or the app goes to the background then android operating system does not let it run for less than every minute. It is triggered after every 60 seconds.
I know it is android's restriction.
My questions is that is there someway to by pass it or some trick that can make it run every 30 seconds.
public static void startBTService()
{
Utilities.showDebugInfo("Start BT Service called...");
if ( Utilities.getCurrentBTService() != null )
{
Utilities.showDebugInfo("BT Service already running...");
Utilities.sendDataAckUI("BT Service already running...", false);
Utilities.runJavascript("showBTServiceButtons(true);");
return;
}
AlarmManager alarmManager = (AlarmManager)((Activity)Utilities.context).getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Utilities.context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Utilities.context, 1111, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, pendingIntent);
Utilities.runJavascript("showBTServiceButtons(true);");
Utilities.sendDataAckUI("BT Service started...", false);
}
Please help.
Thanks
I had a similar problem using the AlarmManager. The solution I used was a Timer queued up inside an IntentService.
int delay = 1000 * 15; // delay for 15 sec.
int period = 1000 * transmitInterval; // interval to send data
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
transmitData();
}
}, delay, period);

Timer task is misbehaving in service

I am using timer task to execute asynctask at fixed interval of time the time five minutes. but timer task sometimes started to misbehave like it doesn't get
at fixed delay in fact it get executed after 10 minutes twice instead of getting every 5 minute why so?
Here is my code
Timer timer = new Timer();
mytimer Mytimer = new mytimer();
timer.scheduleAtFixedRate(Mytimer, 300000, 300000);
class Mytimer extends TimerTask {
#Override
public void run() {
// TODO Auto-generated method stub
new DetailPosition().execute();
}
}
So help me in this
If the timer task is scheduled on a thread pool where all the threads are in use, then it will have to wait for one to become available before running your task.
Consider the following -
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(() -> {Thread.sleep(500); System.out.println(new Date());}, 0, 200, TimeUnit.MILLISECONDS);
Here the task takes longer than the interval, so the tasks will just queue up until you run out of memory.

Multiple timer Task on same service conflicting and misbehaving

Since i have started using two timer task on same service it started misbehaving.
As i am calling api in both timer task 1 is calling api after 30 seconds and another timertask is calling and api after 5 minutes. now they are misbehaving in the way that some times timer task which call api after 5 minutes started calling an api at any timer instead of after 5 minutes. Some times it hits an api 10-15 times between 5 minutes and some timer it hits an api once in after 20-25 minutes.
Here is my code of timer task
Timer timer, timer_update_current_position;
if (timer == null) {
timer = new Timer();
mytimer Mytimer = new mytimer();
timer.scheduleAtFixedRate(Mytimer, 300000, 300000);
}
if (timer_update_current_position == null) {
timer_update_current_position = new Timer();
TimerCurrentPos timerCurrentPos = new TimerCurrentPos();
timer_update_current_position.scheduleAtFixedRate(timerCurrentPos,
30000, 30000);
}
Please help me how to resolve this issue because its adding data in bult on my server.

Categories

Resources