Reputation: 943
I am creating an app that needs to find the location of the local device and compare it to the location of another device. I am attempting to use the Google Play Services location apis to find the locations of the devices but it seems like it just takes way too long to connect. I haven't seen it connect yet. It is either really slow or something isn't setup correctly. Are these apis normally pretty quick when connecting?
Here is what I have:
public class LocationHandler implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
private Activity currActivity = null;
private GoogleApiClient mGoogleApiClient = null;
public LocationHandler(Activity act)
{
currActivity = act;
}
public void Setup()
{
if (mGoogleApiClient == null)
{
mGoogleApiClient = new GoogleApiClient.Builder(currActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
Toast.makeText(currActivity, "Connected to service.", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
}
I have the following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET" />
I have also added this to the build.gradle:
compile 'com.google.android.gms:play-services:9.2.0'
Upvotes: 0
Views: 99
Reputation: 1014
In this case I would use the LocationManager:
public class LocationHandler implements implements LocationListener{
LocationManager mLocationManager;
public void Setup(){
mLocationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 10, this);
}
@Override
public void onLocationChanged(Location location) {
//do something with your location
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
}
}
This approach is straight forward and allows you to skip dealing with Google Play Services.
Upvotes: 1