In my application, I am trying to use a LocationService based on the Google tutorial “Getting Current Location” ( http://developer.android.com/training/location/retrieve-current.html ). Unfortunately, when I really need this, I get 'IllegalStateException: Not connected. Call connect () and wait for the onConnected 'exception . I need a location service for a very short period - when I click some buttons, I want to get the current location (or the last known location). My LocationHandler code is as follows:
class LocationHandler implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private MainActivity m_ma;
private LocationClient m_locationClient;
private boolean m_isAvailable = false;
LocationHandler(MainActivity ma) {
m_ma = ma;
m_locationClient = new LocationClient(ma, this, this);
}
@Override
public void onConnected(Bundle dataBundle) {
Toast.makeText(m_ma, "Connected", Toast.LENGTH_SHORT).show();
m_isAvailable = true;
Log.i(TAG, "LOCATION = " + m_locationClient.getLastLocation());
}
@Override
public void onDisconnected() {
Toast.makeText(m_ma, "Disconnected."
Toast.LENGTH_SHORT).show();
m_isAvailable = false;
Log.i(TAG, "LOCATION dis-connected");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
m_isAvailable = false;
Log.i(TAG, "LOCATION failed");
... code from tutorial
}
void connectLocationClient() {
m_locationClient.connect();
}
void diconnectLocationClient() {
m_locationClient.disconnect();
}
Location getCurrentLocation() {
connectLocationClient();
if(m_isAvailable) {
Log.i(TAG, "3333333333333333 m_locationClient.isConnected() = " + m_locationClient.isConnected());
return m_locationClient.getLastLocation();
} else {
Log.i(TAG, "444444444444");
Toast.makeText(m_ma, "Location Services is not yet avaialable",
Toast.LENGTH_SHORT).show();
return null;
}
}
}
onClick getCurrentLocation().
, , "... " Toast.
onConnected(..) callback getLastLocation() .
, , IllegalStateException, LocationClient
.
, , onConnected() , onDisconnected(), onConnectionFailed(..)?