2

I am busy with a application for my phone for a project. I am not a programmer, so have learnt a bit of java for android so far.

I am stuck on running a method every 60 Seconds while the application is running on the phone.

The application uses the GPS and then sends a User Id & Gps co-ords to a server.

So I have a Method (getLoc) that gets the Location & then calls the send to server method and save to SD card method.

This is for proof of concept & I only need to run the application over the next few days in my car while the phone is connected to the car charger & not allowing it to sleep. I need to log some "test" data (GPS Coords) while I drive around over the next few days.

I am just looking for the easiest way to repeat the method every 60 seconds that sends the data to the server while the Location manager runs & gets the location constantly..

How would I keep the method getLoc to run every 60 Seconds?

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
Andrewbuch
  • 31
  • 2
  • 5

6 Answers6

4

A few problems with this approach:

  1. You can not gather location data all the time, because when the device goes to sleep mode user processes stop executing and network is suspended.

  2. Running GPS all the time will drain battery in a matter of hours.

  3. If you force to use network every 60sec (via AlarmManager, waking the phone), then network will never shutdown and this will kill battery even faster then GPS.

  4. Do you want to update the data even if user does not move? Because, on average, users do not move often.

If you want to handle location data the right way, then it's more complex than simply gathering location at 60 sec interval. I highly suggest you read this excellent blog post: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

And the part 2: http://blog.radioactiveyak.com/2011/06/deep-dive-into-location-part-2-being.html

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Hi. Sorry Should have mentioned that this is for a proof of concept, So just need it to run on the phone plugged into my car charger while I log some "test" data points while driving over the next few days. So battery usage does not matter, I just want the easiest way to run the method every 60 seconds while the location manager is getting the location all the time – Andrewbuch Oct 03 '11 at 19:18
  • @Andrewbuch those are excellent articles, you should definitely follow them – Hades Oct 03 '11 at 19:33
  • @Andrewbuch: In this you can use any of the solutions provided in other answers. I'd go with Timer and TimerTask. – Peter Knego Oct 03 '11 at 19:40
  • I added this under the on create : new Timer().scheduleAtFixedRate(locGet(provider, location), 0, 60000); and then the method locGet is like this : public TimerTask locGet(String prov, Location location). But when the application runs it gets an error "Application has stopped unexpectedly" Force close – Andrewbuch Oct 03 '11 at 21:08
1

Please see this question, and this reference doc for the Android Timer.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
1

Check out the Handler class for delayed method execution, but if you're executing a method every 60 seconds say goodbye to your battery.

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)

Rich
  • 36,270
  • 31
  • 115
  • 154
1

The "easy" answer for repeating a task over the time:

final static long REFRESH=60*1000;
final static int SUBJECT=0;
Handler locationHandler= new Handler(){
    public void handleMessage(Message msg){
        if (msg.what==SUBJECT){
            getLoc();
            this.sendEmptyMessageDelayed(SUBJECT, REFRESH);
        }
    }
};

For stating the track:

       locationHandler.sendEmptyMessage(SUBJECT);

for stopping it:

       locationHandler.removeMessages(SUBJECT);

But this solution is not optimal and will stop working if the phone goes to sleep mode. For doing it correctly you should make a LocationListener or an AlarmManager.

As the other posts say 60 seconds as refresh will vaporize the battery

Addev
  • 31,819
  • 51
  • 183
  • 302
0
//Method for the visibility of Resend button after 60 seconds passed

private void VisibleBtnResend60() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {

                //call anything you want to perform

                mResendButton.setVisibility(View.VISIBLE);
                enableViews(mResendButton);
            }
        }, 60000);
    }
meyasir
  • 268
  • 3
  • 12
0

In Java you would create a Thread running in the background.

I'm not sure that is the right way to do it on an Android.

Do you want this to only run while the user is looking at the application or also while it's still running in the background?

EDIT

Actually, instead of Thread use java.util.Timer and java.util.TimerTask.

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59