Created
November 12, 2016 13:48
-
-
Save tassioauad/942399aeac6a7a74436d62c501cad2d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
private GoogleApiClient googleApiClient; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
googleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) //Be aware of state of the connection | |
.addOnConnectionFailedListener(this) //Be aware of failures | |
.build(); | |
//Trying to connect with Google API. If it works onConnected() will be called, else onConnectionFailed() will be called. | |
googleApiClient.connect(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
stopGoogleApiConnection(); | |
} | |
public void stopGoogleApiConnection() { | |
//Stopping the Connection | |
if (googleApiClient.isConnected()) { | |
googleApiClient.disconnect(); //Closing connection with Google APIs | |
} | |
} | |
/** | |
* After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed. | |
* After this callback, the application can make requests on other methods provided by the client and expect that no user intervention | |
* is required to call methods that use account and scopes provided to the client constructor. | |
* | |
* @param bundle | |
*/ | |
@Override | |
public void onConnected(Bundle bundle) { | |
//Google API connection has been done successfully | |
} | |
/** | |
* Called when the client is temporarily in a disconnected state. | |
* This can happen if there is a problem with the remote service (e.g. a crash or resource problem causes it to be killed by the system). | |
* When called, all requests have been canceled and no outstanding listeners will be executed. | |
* GoogleApiClient will automatically attempt to restore the connection. | |
* Applications should disable UI components that require the service, and wait for a call to onConnected(Bundle) to re-enable them. | |
*/ | |
@Override | |
public void onConnectionSuspended(int i) { | |
//Wait for the GoogleApiClient restores the connection. | |
} | |
/** | |
* Called when there was an error connecting the client to the service. | |
* | |
* @param connectionResult | |
*/ | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
//Google API connection has been failed! Stop it and warn! | |
stopGoogleApiConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment