geolocation - How to continuously track the location of an Android mobile phone? -


i need write app, every 5 minutes determines current location of mobile phone (using free, available location providers) , sends server.

if location provider doesn't work @ start of application, becomes available later, application should process location data well.

in order this, implemented following class. in 1 of activities, create instance of , call starttrackingupdates method. locationchangehandler processing of location data.

public class locationtracker implements ilocationtracker, locationlistener {     public static final long min_time_between_updates = 1000 * 60 * 5; // 5 minutes     public static final long min_distance_change_for_updates = 10; // 10 meters     private ilocationmanager locationmanager;     private ilocationchangehandler locationchangehandler;      public locationtracker(final ilocationmanager alocationmanager,             final ilocationchangehandler alocationchangehandler) {         this.locationmanager = alocationmanager;         this.locationchangehandler = alocationchangehandler;     }      public void starttrackingupdates() {         final list<string> providers = locationmanager.getallproviders();          (final string curprovidername : providers)         {             final ilocationprovider provider = locationmanager.getlocationprovider(curprovidername);              if (!provider.hasmonetarycost())             {                 subscribetolocationchanges(provider);             }         }     }      private void subscribetolocationchanges(final ilocationprovider aprovider) {         locationmanager.requestlocationupdates(aprovider.getname(), min_time_between_updates,                  (float)min_distance_change_for_updates, this);     }      public void onlocationchanged(final location alocation) {         locationchangehandler.processlocationchange(new locationwrapper(alocation));     }      public void onproviderdisabled(final string aprovider) {     }      public void onproviderenabled(final string aprovider) {     }      public void onstatuschanged(final string aprovider, final int astatus,              final bundle aextras) {     } } 

i built application , installed on mobile phone. then, in morning took phone, went work, went home. @ home checked results of day-long work of application , found 1 record in database.

provided rest of system (transmission of location data server, saving in database) works correctly, must have problem in location tracking code (onlocationchanged wasn't invoked though changed location several times).

what's wrong code (how should change it, in order onlocationchanged called whenever location of phone changes more min_distance_change_for_updates meters according 1 of locatin providers) ?

update 1 (18.08.2013 13:59 msk):

i changed code according msh recommendations. start timer using following code:

public void startsavinggeodata() {     final ialarmmanager alarmmanager = activity.getalarmmanager();     final ipendingintent pendingintent = activity.creatependingresult(             alarm_id, intentfactory.createemptyintent(), 0);     alarmmanager.setrepeating(interval, pendingintent); } 

in activity put following timer event handler:

@override protected void onactivityresult(final int arequestcode, final int aresultcode,          final intent adata) {     geodatasender.processonactivityresult(arequestcode, aresultcode,              new intentwrapper(adata)); } 

when phone turned on, works expected - onactivityresult executed once in interval milliseconds.

but when press power button (screen becomes disabled), onactivityresult not invoked @ all. when press power button again, onactivityresult executed many times, interval has passed since time when turned phone off. if turned phone off 1 * interval milliseconds, fire once. if turned phone off 2 * interval milliseconds, fire twice etc.

note: "turning off" mean short press of power button (the phone still "lives" , reacts incoming sms, example).

how should modify code of startsavinggeodata in order method onactivityresult executed every interval milliseconds, when phone sleeps?

update 2 (18.08.2013 19:29 msk): neither alarmmanager.setrepeating(alarmmanager.rtc_wakeup, 0, interval, pendingintent), nor alarmmanager.setrepeating(alarmmanager.rtc_wakeup, interval, interval, pendingintent) solved problem.

your code correct, location providers not running when phone sleeping. may need acquire wakelock (if don't care power draw), or use alarmmanager , schedule application wake , check location periodically (you still need have active wakelock while waiting update, either acquired alammanager, or own).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -