0

I'd like to upload the current user's location about every 2 minutes when device is awake, or about every 5 minutes when not.

What would be the best way to upload the data in the background (even if app is not running) to a web server? Is an IntentService with AlarmManager or something like an AsyncTask with a CountdownTimer better?

I already know how to get a user's location, and have some practice with AsyncTask and IntentService. Any help would be appreciated! Thanks!

3 Answers3

0

Since you want to do a task within a specific period of time, I recommend you use a Firebase Job Scheduler and an IntentService.

Using Firebase Job Dispatcher has a lot of merits.

  1. You can give it constraints like "Do a task only on battery or Do a task when charging and with wifi"
  2. This has efficiency built-in

You can read more about Firebase Job Dispatcher here

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Sriram R
  • 2,109
  • 3
  • 23
  • 40
0

I would recommend Firebase JobDispatcher.

It has some advantages over AlarmManager, Jobschedular and GCMNetworkManager. Read this blog for details.

Usage:

Add the following to your build.gradle's dependencies section:

implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

Create a new MyJobService class.

public class MyJobService extends JobService {
    @Override
    public boolean onStartJob(JobParameters job) {
        // Do some work here

        return false; // Answers the question: "Is there still work going on?"
    }

    @Override
    public boolean onStopJob(JobParameters job) {
        return false; // Answers the question: "Should this job be retried?"
    }
}

Adding it to the manifest

    <service
        android:exported="false"
        android:name=".MyJobService">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
        </intent-filter>
    </service>

Scheduling a job

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

Job myJob = dispatcher.newJobBuilder()
        // the JobService that will be called
        .setService(MyJobService.class)
        // uniquely identifies the job
        .setTag("my-unique-tag")
        // one-off job
        .setRecurring(false)
        // don't persist past a device reboot
        .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
        // start between 0 and 60 seconds from now
        .setTrigger(Trigger.executionWindow(0, 60))
        // don't overwrite an existing job with the same tag
        .setReplaceCurrent(false)
        // retry with exponential backoff
        .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
        // constraints that need to be satisfied for the job to run
        .setConstraints(
                // only run on an unmetered network
                Constraint.ON_UNMETERED_NETWORK,
                // only run when the device is charging
                Constraint.DEVICE_CHARGING
        )
        .build();

dispatcher.mustSchedule(myJob);
Zeero0
  • 2,602
  • 1
  • 21
  • 36
0

There are two better ways that will help you:

1. When app is awake

You can use handler for timer purpose and from the handle call IntentService for location update. IntentService will work in different Thread and will finished itself after work completion. Handler Example

2. When app is not running

You have to use AlarmManager and from the Alarm Receiver you will call IntentService for location update. AlarmManager Example

Hope it will help you.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40