How do I repeat a method every 10 minutes after a button press and end it on another button press
I am writing an Android app that retrieves the phone's current location and sends it too a webserver. I want to be able to press a start button and have the app continue to retrieve and send the location at a predetermined interval (say every 10 minutes) and then have it stop on another button press.
Here is the code for my buttons:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton.setOnClickListener(new OnClickListener() {
@Override
//When the button is clicked
public void onClick(View v) {
finishButton.setEnabled(true);
startButton.setEnabled(false);
//Loops every 10mins
pingCurrentLocation();
}
});
finishButton.setOnClickListener(new OnClickListener() {
@Override
//When the button is clicked
public void onClick(View v) {
startButton.setEnabled(true);
finishButton.setEnabled(false);
pingCurrentLocation();
}
});
}
pingCurrentLocation is the function that gets the location and sends it.
I know that using an AlarmManager would probably achieve what I want but I have been unable to make sense of any of it. Are there any clear steps or templates that will work in my situation.
Create a BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//get and send location information
}
}
and add the same to your AndroidManifest
so that the Receiver is registered
<receiver
android:name="com.coderplus.AlarmReceiver"
android:exported="false">
</receiver>
Now you can set a repeating alarm from your Activity
which will invoke the receiver every 10 minutes:
AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),600000,
pendingIntent);
and to cancel the alarm, call cancel()
on the AlarmManager
using an equivalent PendingIntent
AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.cancel(pendingIntent);
or if you don't want to use AlarmManager
/ BroadcastReceiver
, then something like this will help you out. Before you go for it, check - difference between timer and alarmmanager
private class MyTimerTask extends TimerTask {
@Override
public void run() {
//get and send location information
}
}
initialize the Timer
and Timer
task:
Timer myTimer = new Timer();
MyTimerTask myTimerTask= new MyTimerTask();
Stopping or starting the Timer
//to Stop
myTimer.cancel();
//to start
myTimer.scheduleAtFixedRate(myTimerTask, 0, 600000); //(timertask,delay,period)
Refer http://developer.android.com/reference/java/util/TimerTask.html
http://developer.android.com/reference/java/util/Timer.html
Use Android-TimerTask or Android-AlarmManager for sending location data to every 10 minutes. Look at this SO question Track Gps At every 10 minutes using timer in android also this one Good way of getting the user's location in Android
TimerTask:
The TimerTask
class represents a task to run at a specified time. The task may be run once or repeatedly.
Here is what I did.
created an alarm manager object first, and set the repeating timer
AlarmManager alarmMgr = alarmMgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = alarmIntent = new Intent("AlarmIntentReceiver");
PendingIntent pendingAlarmIntent = pendingAlarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, alarmIntent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 30*1000, 3*60*1000, pendingAlarmIntent); //start in 30 secs and rest in 3 mins interval
Created an activity based on that intent name that will capture the intent and execute it's code in the interval specified, you can also create a broadcast receiver if you like.
To cancel it in your button click event. Write this
alarmMgr.cancel(pendingAlarmIntent);