0

I want to write a location based Client/Server application - Java/Android. I'm new to this field. The application use the user location to provide it some information when he ask it. Should I run the "client" object that interacts with the server as a "service" from the main thread - UI thread of android app? (I thought maybe it can be useful for synching information with the server, like updating the server with GPS location and save the information relevant to this location in cache in the device, maybe update application version, and maybe things that will be useful and I cannot think about them now)

First, is this the right way to do it - using service class? Basically, the information can be given to the client only when he asks it specifically, but I thought maybe this way, I can improve the client experience (of course in the limits of the server ability to process the information from all the users)

second, is updating the server with the location is an operation that consumes a lot of battery? more than "regular applications" running on the device, like email, facebook, waze in non-navigating mode etc?

Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61

2 Answers2

1

You can possibly use this location provider class to help you:

public class LocationProvider implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

public abstract interface LocationCallback {
    public void handleNewLocation(Location location);
}

public static final String TAG = LocationProvider.class.getSimpleName();

/*
 * Define a request code to send to Google Play services
 * This code is returned in Activity.onActivityResult
 */
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

private LocationCallback mLocationCallback;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

public LocationProvider(Context context, LocationCallback callback) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationCallback = callback;

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)
            .setFastestInterval(1 * 1000);

    mContext = context;
}

public void connect() {
    mGoogleApiClient.connect();
}

public void disconnect() {
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

public void refresh() {
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Location services connected.");

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    else {
        mLocationCallback.handleNewLocation(location);
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    /*
     * Google Play services can resolve some errors it detects.
     * If the error has a resolution, try sending an Intent to
     * start a Google Play services activity that can resolve
     * error.
     */
    if (connectionResult.hasResolution() && mContext instanceof Activity) {
        try {
            Activity activity = (Activity)mContext;
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        /*
         * Thrown if Google Play services canceled the original
         * PendingIntent
         */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
        /*
         * If no resolution is available, display a dialog to the
         * user with the error.
         */
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

@Override
public void onLocationChanged(Location location) {
    mLocationCallback.handleNewLocation(location);
}
}

And use the .refresh method to get location updates to possibly store however and where ever you would like.

As for the battery concern, Google Play Services does use battery to request location periodically but it shouldn't be that noticeable to the user or to you as it is minor compared to other battery using mechanisms of the phone such as the screen.

Benyam Ephrem
  • 448
  • 6
  • 20
  • LocationProvider is a class you wrote, it is not related to android.location.LocationProvider. just to make sure – Day_Dreamer Mar 09 '15 at 11:28
  • and one little thing, LocationListener is an interface that has 4 abstract methods, shouldn't them be all implemented in order to implement the interface? – Day_Dreamer Mar 09 '15 at 12:11
  • @Day_Dreamer What method was not implemented? – Benyam Ephrem Mar 09 '15 at 12:14
  • http://developer.android.com/reference/android/location/LocationListener.html#onLocationChanged(android.location.Location) im the public method summary I see 4 abstract – Day_Dreamer Mar 09 '15 at 12:17
  • 1
    @Day_Dreamer I don't believe you have to implement all of the methods. Only the ones you need which is `onLocationChanged` but you could implement the others to have a complete interface. – Benyam Ephrem Mar 09 '15 at 12:33
  • 1
    @Day_Dreamer The other implemented interfaces achieve what you need to have a functioning class that gets location which is essentially our main objective here. If I am wrong about any of this please tell me because I don't want to be telling you anything wrong. – Benyam Ephrem Mar 09 '15 at 12:36
  • 1
    @Day_Dreamer And no this class does not implement android.location.LocationProvider so it is not related to it. – Benyam Ephrem Mar 09 '15 at 12:46
  • I'll check this and I'll return to you, if something :) thanks again – Day_Dreamer Mar 09 '15 at 13:09
  • 1
    http://stackoverflow.com/questions/11437097/not-implementing-all-of-the-methods-of-interface-is-it-possible – Day_Dreamer Mar 09 '15 at 13:12
1

I have worked with such kind of location based application. The best way to fetch location would be fetch it every time location is changed via onLocationChanged() callback implementing LocationListener interface. And also fetch the location when ever user wants it using LocationManager class. This way you can get the updated location also and save some battery too. I have some implementation for my application which you can use as reference : https://github.com/kodered/TrackMe-Ver-2.0 Hope this helps !

Saurav
  • 560
  • 4
  • 17