Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9404

android run application always even if application in background

I want to make the following:

1]- listen to the change of the GPS location of the phone and send it to server to track user location continuously

2]- I have found an example to find the GPS location using LocationListener

3]- I have found a way to open my application when device restart

I need some help to be able to send this data even if user put the application in background

Any help here?

Upvotes: 1

Views: 1197

Answers (1)

Guy S
Guy S

Reputation: 1424

this service should work in the background

The LocationClient is the main entry point for location related APIs, such as location and geofence.

Use the LocationClient to:

  • Connect and disconnect to Google Location Services.
  • Request/remove location update callbacks.
  • Request/remove geofences.

In order to establish a connection, call connect() and wait for the onConnected(android.os.Bundle) callback.

LocationRequest objects are used to request a quality of service for location updates from the LocationClient.

in LocationRequest, you can set parameters there such as the accuracy of the location and time interval between location updates.

onLocationChanged will get called according to time interval you set in LocationRequest and from there you can update your server. the service does not run in the background so you will need to update the server with AsyncTask or some other way, just make sure the server updates are done on a background thread.

public class LocationUpdatesService extends Service implements GooglePlayServicesClient.ConnectionCallbacks,
                                                             GooglePlayServicesClient.OnConnectionFailedListener,
                                                             LocationListener {
private static int LOCATION_UPDATE_INTERVAL = 30000; // how often you will get a location update (this is in milliseconds)
private LocationClient locationClient;
private LocationRequest locationRequest;
private boolean isConnected = false;

@Override
// onCreate is called when the service gets started (from an Activity) than immediately calls onStartCommand
public void onCreate() {
    super.onCreate();

    if (servicesConnected()) {
        startLocationUpdates();
    } else {
        // isGooglePlayServicesAvailable FAILURE
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}

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

private boolean servicesConnected() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        return false;
    }
}

public void startLocationUpdates() {

    locationRequest = LocationRequest.create();
    locationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setFastestInterval(LOCATION_UPDATE_INTERVAL);
    locationClient = new LocationClient(this, this, this);
    locationClient.connect();
    isConnected = true;
}

@Override
public void onDestroy() {
    if (locationClient.isConnected()) {
        onDisconnectClient();
    } else {
        // locationClient is disconnected
    }
    super.onDestroy();
}

private void onDisconnectClient() {
    isConnected = false;
    locationClient.removeLocationUpdates(this);
    locationClient.disconnect();
    locationRequest = null;
    locationClient = null;
}


@Override
public void onLocationChanged(Location location) {
    // update server from here with AsyncTask (or some other way but in the background)
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnected(Bundle bundle) {
    locationClient.requestLocationUpdates(locationRequest, this);   
}

@Override
public void onDisconnected() {
}

}

helpful links:

http://developer.android.com/reference/android/app/Service.html

http://developer.android.com/guide/components/services.html

http://developer.android.com/reference/android/location/LocationListener.html

https://developer.android.com/reference/com/google/android/gms/location/LocationRequest.html

https://developer.android.com/reference/com/google/android/gms/location/LocationListener.html

Upvotes: 1

Related Questions