Skip to content

Commit dde62ab

Browse files
author
hussienalrubaye
committed
play services
1 parent 39651bd commit dde62ab

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

GooglePlayServices.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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'

MyTracker/app/src/main/java/com/alrubaye/mytracker/MapsActivity.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.provider.ContactsContract;
55
import android.support.v4.app.FragmentActivity;
66
import android.os.Bundle;
7+
import android.view.KeyEvent;
78

89
import com.google.android.gms.maps.CameraUpdateFactory;
910
import com.google.android.gms.maps.GoogleMap;
@@ -25,24 +26,26 @@ public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
2526

2627
private GoogleMap mMap;
2728
DatabaseReference databaseReference;
29+
2830
@Override
2931
protected void onCreate(Bundle savedInstanceState) {
3032
super.onCreate(savedInstanceState);
3133
setContentView(R.layout.activity_maps);
32-
databaseReference= FirebaseDatabase.getInstance().getReference();
3334
Bundle b=getIntent().getExtras();
35+
databaseReference= FirebaseDatabase.getInstance().getReference();
3436
LoadLocation(b.getString("PhoneNumber"));
3537

3638
}
3739

3840
void LoadLocation(String PhoneNumber){
41+
3942
databaseReference.child("Users").child(PhoneNumber).
4043
child("Location").addValueEventListener(new ValueEventListener() {
4144
@Override
4245
public void onDataChange(DataSnapshot dataSnapshot) {
4346

4447
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
45-
48+
if (td==null)return;
4649
double lat = Double.parseDouble(td.get("lat").toString());
4750
double lag = Double.parseDouble(td.get("lag").toString());
4851
/** Make sure that the map has been initialised **/
@@ -89,4 +92,9 @@ public void onMapReady(GoogleMap googleMap) {
8992
mMap.addMarker(new MarkerOptions().position(sydney).title("last online:"+ LastDateOnline));
9093
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,15));
9194
}
95+
96+
97+
98+
99+
92100
}

0 commit comments

Comments
 (0)