Commit 76c859c0 authored by Paded's avatar Paded

fix update location onChange only

parent 6f49af81
......@@ -10,6 +10,7 @@ import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.Manifest;
......@@ -21,7 +22,9 @@ import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
......@@ -45,9 +48,10 @@ import ubontransitdriver.paded.com.ubontransitdriver.GEOFENCE.GeofenceRegistrati
public class TrackerService3 extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient.OnConnectionFailedListener, LocationListener {
FusedLocationProviderClient mFusedLocationClient;
GoogleApiClient mGoogleApiClient;
private GoogleApiClient googleApiClient;
LocationRequest mLocationRequest;
String TAG = "trackService";
private String user_id;
......@@ -104,7 +108,7 @@ public class TrackerService3 extends Service implements
// Log.d(TAG, "onStartCommand: "+user_id+" "+bus_id);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (mGoogleApiClient != null && mFusedLocationClient != null) {
requestLocationUpdates();
// requestLocationUpdates();
} else {
buildGoogleApiClient();
}
......@@ -112,10 +116,20 @@ public class TrackerService3 extends Service implements
}
protected synchronized void buildGoogleApiClient() {
// mGoogleApiClient = new GoogleApiClient.Builder(this)
// .addConnectionCallbacks(this)
// .addOnConnectionFailedListener(this)
// .addApi(LocationServices.API)
// .build();
// mGoogleApiClient.connect();
//////////////////////////////////////
// Create Google API Client instance
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
......@@ -275,7 +289,8 @@ public class TrackerService3 extends Service implements
super.onDestroy();
if (mFusedLocationClient != null) {
Log.d(TAG, "Service Stop");
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
// mFusedLocationClient.removeLocationUpdates(mLocationCallback);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
......@@ -322,7 +337,8 @@ public class TrackerService3 extends Service implements
}
;
};
......@@ -346,10 +362,35 @@ public class TrackerService3 extends Service implements
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationUpdates();
// requestLocationUpdates();
// Log.d(TAG, "Google Api Client Connected");
// isMonitoring = true;
// // startGeofencing();
//////////////////////////////////////////////
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationAvailability locationAvailability = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
if (locationAvailability.isLocationAvailable()) {
// Call Location Services
Log.d(TAG, "Google Api Client Connected");
isMonitoring = true;
// startGeofencing();
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(100000);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
} else {
// Do something when Location Provider not available
}
}
......@@ -364,4 +405,27 @@ public class TrackerService3 extends Service implements
isMonitoring = false;
}
@Override
public void onLocationChanged(Location location) {
final String path = "active_bus/" + user_id;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
ref.child("lat").setValue(location.getLatitude());
ref.child("lng").setValue(location.getLongitude());
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
ref.child("time").setValue(formattedDate);
ref.child("bus_id").setValue(bus_id);
ref.child("destination").setValue(destination);
ref.child("start_busstop_id").setValue(start_busstop_id);
ref.child("stop_busstop_id").setValue(stop_busstop_id);
Log.d(TAG, "onLocationChanged: "+location.getLatitude()+" "+location.getLongitude());
}
}
......@@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-alpha06'
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
......
#Mon Jan 15 11:01:57 ICT 2018
#Tue May 29 16:02:46 ICT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-rc-3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment