Manu
Manu

Reputation: 11

Getting Current Location inside a Service in android

I'm trying to get the current GPS location of a phone inside a android service. I try to do it like here:

Getting Repeated Current Location inside a Service in android?

My service looks like this:

public class MeasurementService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener  {

private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private Feedback feedback;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();
    mGoogleApiClient.connect();
    feedback = new Feedback();

    if (isNetworkAvailable()) {
        new MeasuringTask().execute();
    }
    return Service.START_NOT_STICKY;
}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}

@Override
public void onConnected(Bundle arg0) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(2000); 
    ...
}

@Override
public void onConnectionSuspended(int arg0) {

}

@Override
public void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.disconnect();
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

private class MeasuringTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      //network access
    }


}

I start my service through a broadcast receiver everytime the phone is started. When I execute the app I get a java.lang.NoClassDefFoundError: packet.MeasurementService

If I try to change this part:

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();

To this:

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
    .addConnectionCallbacks(MeasurmentService.class)
    .addOnConnectionFailedListener(MeasurmentService.class)
    .addApi(LocationServices.API)
    .build();

Like recommended in the other post, I get:

The method addConnectionCallbacks(GoogleApiClient.ConnectionCallbacks) in the type GoogleApiClient.Builder is not applicable for the arguments (Class)

Ideas? Thanks a lot!

Upvotes: 1

Views: 365

Answers (2)

Manu
Manu

Reputation: 11

I finally figured it out, it was a build error which I wasn't able to resolve.

My solution:

I migrated the project from Eclipse to Android Studio.

Upvotes: 0

Ke Di
Ke Di

Reputation: 345

Make sure the LocationListener your service implemented is from com.google.android.gms.location.

Move the code to onCreate function.

    @Override
    public void onCreate() { 
        mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();

                mGoogleApiClient.connect();
}

I have a sample application on github, please click here.

Upvotes: 1

Related Questions