Reputation: 1947
I'm currently developing an application and I'm using this post code to get the GPS lock. What is the simplest and most robust way to get the user's current location on Android?
It works like a charm! It chooses very well for my requirements. What I'm now trying to do is triggering this method every 10 minutes to get gps signal and saving it in a txt file. My problem is I've been unable to understand how AlarmManager works as it calls AFAIK the onCreate() method of the activity. How can I make an AlarmManager call an specific method every 10 minutes in the background even with the telephone sleeping?
Edit 2: I've used the code provided by A--C but for some reason the alarm isn't calling the method on my class AlarmBroadcastReceiver. This is my method in the MainActivity.
Intent intent = new Intent (this, AlarmBroadcastReceiver.class);
PendingIntent pendIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService (Context.ALARM_SERVICE);
alarmManager.cancel(pendIntent); //cancel if active already
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+4000,
4000, pendIntent);
and in the AlarmBroadcastReceiver class.
public class AlarmBroadcastReceiver extends BroadcastReceiver{
public float accuracy;
public double latitude;
public double longitud;
public static boolean Activado=false;
public void onReceive(Context context, Intent intent) {
Log.d("AlarmManager", "Method onReceiveCalled");
}
What I'm doing wrong?
Upvotes: 1
Views: 3368
Reputation: 36449
Unless the Activity
's method is static and is running in the same process, you don't run a single method via Intents.
You could pass of extras to your Activity through its Intent that specifies what it should be doing and in onCreate()
take care of the logic for that.
You could also try to put the functionality directly in the AlarmReceiver, since that's as local as you can go for that class.
So of course, you could replace the Activity with a background service that handles this.
Lastly, since you're saving to a text file every 10 mins, you could potentially make a class specifically for saving. All you do is call that class with the String
and Context
(I'm assuming file operations need a Context
) from wherever you need. This can be integrated with your GPS code, most likely with another constructor so the GPS class knows it's specifically getting the location and saving it.
Here is an example of how the alarm setup could look like:
Intent intent = new Intent (this, AlarmBroadcastReceiver.class);
PendingIntent pendIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService (Context.ALARM_SERVICE);
alarmManager.cancel(pendIntent); //cancel if active already
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+600000,
600000, pendIntent);
So you need an Intent, it gets turned into a PendingIntent. Then you need to get an AlarmManager instance. The last line of my code registers the alarm 10 minutes ahead of time with a repeat of 10 minutes. AlarmManager.RTC_WAKEUP
forces the phone to wake up to execute the alarm.
Upvotes: 1