2

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();
    }
gaurang
  • 2,217
  • 2
  • 22
  • 44
  • I thnk you can use socket between mobile and server to communicate for real time updates. – Nikhil Sharma Mar 31 '17 at 07:30
  • You need nothing special. You dont need a service running on your device. Just send lat,lon to your server. And if your location listener has a new lat,lon in onLocationChanged then send again. And so on. I dont understand the problem. – greenapps Mar 31 '17 at 07:32
  • i want to send location continuously to server from background – gaurang Mar 31 '17 at 07:33

2 Answers2

1

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);
  • can you give me example or tutorial link? – gaurang Mar 31 '17 at 07:32
  • in this case if i start the service and make api call from oncreate() of service, is it call api continuously? or only once? – gaurang Mar 31 '17 at 09:02
  • it will call only once but you can make your api call continuously by using **Handler** you can get example of Handler from here http://stackoverflow.com/a/40058010/6497550 – Prashant Jaiswal Mar 31 '17 at 09:38
  • do i need to use this handler in onCreate() of service? and make api call inside of handler? right? – gaurang Mar 31 '17 at 09:44
  • yes exactly try it if any errror occurs comment here – Prashant Jaiswal Mar 31 '17 at 09:51
  • i have done this and instead of making api call i have just used Toast message, it is running ok, but i'm getting warning like... E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9f57d1b0 ... so is it making a load on RAM or slowing down the app or device by doing this? – gaurang Mar 31 '17 at 10:13
  • which device you are using and version? – Prashant Jaiswal Mar 31 '17 at 10:31
  • i'm using emulator and version 6.0 – gaurang Mar 31 '17 at 10:46
  • as it is service it will use ram for functioning. but it wont hinder performance of your phone. If your query is solved then mark it green tick so that others who come here for answer gets it. HAPPY CODING. – Prashant Jaiswal Mar 31 '17 at 10:55
0

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();
}
esQmo_
  • 1,464
  • 3
  • 18
  • 43
J. Shiv
  • 144
  • 4