2

My intention is to make an application that will track the movement of my android phone for every few minutes and send it to my server. I have read a lot online on how to do it with a service, AlarmManager and Partial_WakeLock. I have also gone through the commonsware examples in github.com but I was a bit confused because I am still not experienced in android.

I have been successful in getting my application to [get location and send it to my server]. How do I make my service wakeup every few minutes and do [work mentioned]? In the Wakeful example in commonsware, in which method do I mention my [work] and in which method do I keep calling it?

Sap
  • 168
  • 1
  • 16

2 Answers2

7

You need a Service and an AlarmManager. Your Service will handle getting the position and posting it to the server and AlarmManager will invoke your service basen on an interval you decide. You should initialize your AlarmManager with your Service roughly like this in onCreate or other place you want:

AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, YourAlarmReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);

// Use inexact repeating which is easier on battery (system can phase events and not wake at exact times)
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, YOUR_ALARM_TRIGGER_AT_TIME,YOUR_ALARM_INTERVAL, pendingIntent);

YourAlarmReceiver gonna start your service

public class YourAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
          context.startService(new Intent(context, YourService.class));
    }
}

On how to use Services refer to the android website http://developer.android.com/guide/topics/fundamentals/services.html

C.d.
  • 9,932
  • 6
  • 41
  • 51
  • I shall try this out and post here if it goes well. Thank you. – Sap Feb 12 '12 at 01:00
  • I tried it out and the purpose served fine. The problem I am having now is that I am getting too many data points. I ask *LocationManager* to do [work] only when at least 10 meters of displacement is seen. Still, I am getting data points every other second (almost) of the same location until the displacement is seen. Could this be because of the *AlarmManager* ? – Sap Feb 15 '12 at 00:27
  • Are you invoking the LocationManager with the AlarmManager? The interval you have given to the AlarmManager will be used to call the service you like at those intervals... I couldn't understand the problem exactly? Are you saying that you have too much update, if so you can increase the interval.. after getting the result with alarmmanager if therese is no 10 meters of displacement you can dispose the result and I don't know but 10 meters of displacement seems not enough for the gps accuracy. – C.d. Feb 15 '12 at 08:02
  • Ok I should be more clear. I have included the AlarmManager and Receiver as you have described above, giving parameters such that the alarm first goes off 1 minute after I start the service, and will be called at 1 minute intervals. Every time I create a new intent and cancel it with alarmMgr.cancel(pendingIntent); – Sap Feb 17 '12 at 21:00
  • Now, I'm assuming when the alarm goes off, it checks whether the service is on and does nothing if it is on. However, after the service has been on for an hour or more, I'm getting 20 copies of the same location (with different time stamp). My question is does the AlarmManager create new copies of the service everytime and hence the high numbr of data points? – Sap Feb 17 '12 at 21:04
  • I guess it does not start a new service. As you said, it does nothing if the service is already started as far as i know. By the way, 1 minute is a really short interval best practices suggest at least **15 minute** intervals to preserve the battery power. – C.d. Feb 20 '12 at 10:00
0

you could use a partial wakeLock with a sleep(X) which when the sleep(x) is resolved the system will call the next line of code, but the problem is I am seeing a possible infinite cycle that might need a task kill action, or just crash the system.

gardian06
  • 1,496
  • 3
  • 20
  • 34
  • I am going to try this too. The thing is I don't care if it goes into an infinite cycle, because I need data for say, days together. Thank you. – Sap Feb 12 '12 at 01:01