1+ public class GooglePlayServices
2+ implements GoogleApiClient .ConnectionCallbacks ,
3+ GoogleApiClient .OnConnectionFailedListener
4+ {
5+ private final String TAG = "GooglePlayServices:" ;
6+ protected GoogleApiClient mGoogleApiClient ;
7+
8+ Context context ;
9+ public GooglePlayServices ( Context context ) {
10+ this .context =context ;
11+
12+ // build the Play Services client object
13+ mGoogleApiClient = new GoogleApiClient .Builder (context )
14+ .addConnectionCallbacks (this )
15+ .addOnConnectionFailedListener (this )
16+ .addApi (LocationServices .API )
17+ .build ();
18+
19+
20+ }
21+
22+ @ Override
23+ protected void onStart () {
24+ super .onStart ();
25+ Log .i (TAG , "onStart: Connecting to Google Play Services" );
26+
27+ // Connect to Play Services
28+ GoogleApiAvailability gAPI = GoogleApiAvailability .getInstance ();
29+ int resultCode = gAPI .isGooglePlayServicesAvailable (this );
30+ if (resultCode != ConnectionResult .SUCCESS ) {
31+ gAPI .getErrorDialog (this , resultCode , 1 ).show ();
32+ }
33+ else {
34+ mGoogleApiClient .connect ();
35+ }
36+ }
37+
38+ @ Override
39+ protected void onStop () {
40+ super .onStop ();
41+ if (mGoogleApiClient .isConnected ()) {
42+ Log .i (TAG , "onStop: Disconnecting from Google Play Services" );
43+ mGoogleApiClient .disconnect ();
44+ }
45+ }
46+ /**
47+ * Google Play Services Lifecycle methods
48+ */
49+ @ Override
50+ public void onConnected (Bundle connectionHint ) {
51+ Log .i (TAG , "onConnected: Is connected to Google Play Services" );
52+ //TODO: we connected
53+
54+ }
55+
56+ @ Override
57+ public void onConnectionFailed (ConnectionResult result ) {
58+ //error result
59+ Log .d (TAG , "Connection failed: ConnectionResult.getErrorCode() = " + result .getErrorCode ());
60+ }
61+
62+ @ Override
63+ public void onConnectionSuspended (int cause ) {
64+ Log .d (TAG , "Connection was suspended for some reason" );
65+ mGoogleApiClient .connect (); //reconnected
66+ }
67+
68+
69+
70+ }
71+
72+ //TODO: Add play services in Grade
73+ //compile 'com.google.android.gms:play-services:Version'
0 commit comments