gaurang
gaurang

Reputation: 2240

how to send location data (latitude, longitude) periodically to web server

I am developing real time application so i need to send continuous or some periodic (in seconds) location data as latitude and longitude to web service using rest API. so what can i use to send continuous or periodic data to server? do i need to use back ground service or anything else? i don't know how background service work and how to use it? so can anyone help me for this? thanks in advance.

protected void startLocationUpdates() {

        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);

    }

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    }

    @Override
    public void onConnected(Bundle arg0) {


        displayLocation();

        if (mRequestingLocationUpdates) {
            startLocationUpdates();
        }
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;

        Toast.makeText(getApplicationContext(), "Location changed!",
                Toast.LENGTH_SHORT).show();

        displayLocation();
    }

Upvotes: 2

Views: 3286

Answers (2)

J. Shiv
J. Shiv

Reputation: 144

Get the lat and Long Values, then Schedule a periodic task to run the method to send value to the server

you can use Job Scheduler (https://developer.android.com/reference/android/app/job/JobScheduler.html)

or

You can use android-job/evernote to do a periodic Task (https://github.com/evernote/android-job)

private void schedulePeriodicJob() {
    int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5))
            .setPersisted(true)
            .build()
            .schedule();
}

Upvotes: 0

Prashant Jaiswal
Prashant Jaiswal

Reputation: 113

you need to make a service for that if you want to send data continuous to the server even after your application is closed.

Here is how i made my service.

 public class FeatureService extends Service
{

@Nullable
@Override
public IBinder onBind(Intent intent)
{
    return null;
}

@Override
public void onCreate()
{
  // make your api call here and other implementations according to your requirement.
    super.onCreate();

}

@Override
public void onDestroy()
{
   // what you want when service is destroyed 
}

Declare your service in Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="Your Serive"
        android:enabled="true">
    </service>
</application>

And Finally call your service like this in onCreate of your relevant activity.

Intent i= new Intent(this,FeatureService.class);
startService(i);

Upvotes: 1

Related Questions