Last active
August 31, 2016 09:50
-
-
Save goyalsk/324827993f96cd08a63a520991b34acc 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 LocationReceiver extends BroadcastReceiver { | |
private String TAG = "LocationReceiver"; | |
private LocationResult mLocationResult; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if(LocationResult.hasResult(intent)) { | |
this.mLocationResult = LocationResult.extractResult(intent); | |
Log.i(TAG, "Location Received: " + this.mLocationResult.toString()); | |
} | |
} | |
} |
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
/** | |
* | |
* BackgroundLocationService used for tracking user location in the background. | |
* @author cblack | |
*/ | |
public class testService extends Service implements | |
GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, | |
LocationListener { | |
IBinder mBinder = new LocalBinder(); | |
private GoogleApiClient mGoogleApiClient; | |
private PowerManager.WakeLock mWakeLock; | |
private LocationRequest mLocationRequest; | |
Location location = null; | |
// Flag that indicates if a request is underway. | |
private boolean mInProgress; | |
private double myLatitudeLast = 0; | |
private double myLongitudeLast = 0; | |
private double myAltitudeLast = 0; | |
private double mySpeedLast = 0; | |
private double myAltitude = 0; | |
private double mySpeed = 0; | |
private Boolean servicesAvailable = false; | |
// Milliseconds per second | |
private static final int MILLISECONDS_PER_SECOND = 1000; | |
// Update frequency in seconds | |
private static final int UPDATE_INTERVAL_IN_SECONDS = 30; | |
// Update multiplier for Inactive state | |
private static final int UPDATE_MULTIPLIER = 5; | |
// Update frequency in milliseconds | |
public static final long ACTIVE_UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; | |
public static final long INACTIVE_UPDATE_INTERVAL = UPDATE_MULTIPLIER * MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; | |
// The fastest update frequency, in seconds | |
private static final int FASTEST_INTERVAL_IN_SECONDS = 1; | |
// A fast frequency ceiling in milliseconds | |
public static final long ACTIVE_FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; | |
public static final long INACTIVE_FASTEST_INTERVAL = UPDATE_MULTIPLIER * MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; | |
private int delayInterval = 10 * 1000; // delay in milliseconds | |
private int delayIntervalInactive = 5 * 60 * 1000; // delay in milliseconds | |
private int delayIntervalActive = 10 * 1000; // delay in milliseconds | |
private int delay_loc = 10; // distance moved in meter | |
public class LocalBinder extends Binder { | |
public testService getServerInstance() { | |
return testService.this; | |
} | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mInProgress = false; | |
// Create the LocationRequest object | |
mLocationRequest = LocationRequest.create(); | |
// Use high accuracy | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
// Set the update interval to 5 seconds | |
mLocationRequest.setInterval(ACTIVE_UPDATE_INTERVAL); | |
// Set the fastest update interval to 1 second | |
mLocationRequest.setFastestInterval(ACTIVE_FASTEST_INTERVAL); | |
servicesAvailable = servicesConnected(); | |
/* | |
* Create a new location client, using the enclosing class to | |
* handle callbacks. | |
*/ | |
setUpLocationClientIfNeeded(); | |
} | |
/* | |
* Create a new location client, using the enclosing class to | |
* handle callbacks. | |
*/ | |
protected synchronized void buildGoogleApiClient() { | |
this.mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
private boolean servicesConnected() { | |
// Check that Google Play services is available | |
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); | |
// If Google Play services is available | |
if (ConnectionResult.SUCCESS == resultCode) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public int onStartCommand (Intent intent, int flags, int startId) | |
{ | |
super.onStartCommand(intent, flags, startId); | |
PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE); | |
/* | |
WakeLock is reference counted so we don't want to create multiple WakeLocks. So do a check before initializing and acquiring. | |
This will fix the "java.lang.Exception: WakeLock finalized while still held: MyWakeLock" error that you may find. | |
*/ | |
if (this.mWakeLock == null) { //**Added this | |
this.mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); | |
} | |
if (!this.mWakeLock.isHeld()) { //**Added this | |
this.mWakeLock.acquire(); | |
} | |
if(!servicesAvailable || mGoogleApiClient.isConnected() || mInProgress) | |
return START_STICKY; | |
setUpLocationClientIfNeeded(); | |
if(!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) | |
{ | |
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Started", Constants.LOG_FILE); | |
mInProgress = true; | |
mGoogleApiClient.connect(); | |
} | |
return START_STICKY; | |
} | |
private void setUpLocationClientIfNeeded() | |
{ | |
if(mGoogleApiClient == null) | |
buildGoogleApiClient(); | |
} | |
// Define the callback method that receives location updates | |
@Override | |
public void onLocationChanged(Location location) { | |
// Report to the UI that the location was updated | |
String msg = Double.toString(location.getLatitude()) + "," + | |
Double.toString(location.getLongitude()); | |
Toast.makeText(this.getApplicationContext(), | |
"Test Service: OnLocationChange: " + msg, Toast.LENGTH_SHORT).show(); | |
Log.d("debug", msg); | |
// Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); | |
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ":" + msg, Constants.LOCATION_FILE); | |
} | |
@Override | |
public void onStatusChanged(String s, int i, Bundle bundle) { | |
} | |
@Override | |
public void onProviderEnabled(String s) { | |
} | |
@Override | |
public void onProviderDisabled(String s) { | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mBinder; | |
} | |
public String getTime() { | |
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
return mDateFormat.format(new Date()); | |
} | |
/* | |
public void appendLog(String text, String filename) | |
{ | |
File logFile = new File(filename); | |
if (!logFile.exists()) | |
{ | |
try | |
{ | |
logFile.createNewFile(); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
try | |
{ | |
//BufferedWriter for performance, true to set append to file flag | |
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); | |
buf.append(text); | |
buf.newLine(); | |
buf.close(); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
*/ | |
@Override | |
public void onDestroy() { | |
// Turn off the request flag | |
this.mInProgress = false; | |
if (this.servicesAvailable && this.mGoogleApiClient != null) { | |
this.mGoogleApiClient.unregisterConnectionCallbacks(this); | |
this.mGoogleApiClient.unregisterConnectionFailedListener(this); | |
this.mGoogleApiClient.disconnect(); | |
// Destroy the current location client | |
this.mGoogleApiClient = null; | |
} | |
// Display the connection status | |
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": | |
// Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); | |
if (this.mWakeLock != null) { | |
this.mWakeLock.release(); | |
this.mWakeLock = null; | |
} | |
super.onDestroy(); | |
} | |
/* | |
* Called by Location Services when the request to connect the | |
* client finishes successfully. At this point, you can | |
* request the current location or start periodic updates | |
*/ | |
@Override | |
public void onConnected(Bundle bundle) { | |
// Request location updates using static settings | |
Intent intent = new Intent(this, LocationReceiver.class); | |
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; | |
} | |
// LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, | |
// mLocationRequest, this); // This is the changed line. | |
PendingIntent pendingIntent = PendingIntent | |
.getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT); | |
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, | |
mLocationRequest, pendingIntent); // This is the changed line. | |
Log.e(DateFormat.getDateTimeInstance().format(new Date()) + ": Connected", ""); | |
} | |
/* | |
* Called by Location Services if the connection to the | |
* location client drops because of an error. | |
*/ | |
@Override | |
public void onConnectionSuspended(int i) { | |
// Turn off the request flag | |
mInProgress = false; | |
// Destroy the current location client | |
mGoogleApiClient = null; | |
// Display the connection status | |
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); | |
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected", Constants.LOG_FILE); | |
} | |
/* | |
* Called by Location Services if the attempt to | |
* Location Services fails. | |
*/ | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
mInProgress = false; | |
/* | |
* Google Play services can resolve some errors it detects. | |
* If the error has a resolution, try sending an Intent to | |
* start a Google Play services activity that can resolve | |
* error. | |
*/ | |
if (connectionResult.hasResolution()) { | |
// If no resolution is available, display an error dialog | |
} else { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment