I am calling a service were i will get current location at some interval and match the location with a location passed to the service.and if it is in a close range it will buzz the phone.Now i tried to get the current location by FusedLocationApi
.But i got error when i write this portion
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)//here i got the error
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
I got error in the compiler like:
`addConnectionCallbacks` in builder can't be applied to java.lang.Runnable
I tried to give getApplicationContext()
instead of these but same result.I am calling service class from fragment.My whole code:
public class LocBuzzService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static final String TAG = "HelloService";
private boolean isRunning = false;
public Vibrator vibrator;
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
isRunning = true;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStartCommand");
new Thread(new Runnable() {
@Override
public void run() {
try {
//Bundle b=intent.getExtras();
//Double lat = b.getDouble("lat");
//Double lng = b.getDouble("lng");
//vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
//vibrator.vibrate(3000);
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
// stopSelf();
}
}).start();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service onBind");
return null;
}
@Override
public void onDestroy() {
Log.i(TAG, "Service onDestroy");
isRunning = false;
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Onconnected");
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(2000); // Update location every second
//LocationServices.FusedLocationApi.requestLocationUpdates(
//mGoogleApiClient, mLocationRequest,this);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "LocationChanged");
Double lat1 = location.getLatitude();
Double lng1 = location.getLongitude();
Log.i(TAG,String.valueOf(lat1)+ ", " + String.valueOf(lng1));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
What's the problem actually and am i going the right path or is there any easy way to get the current location at some interval??