Note that there are some explanatory texts on larger screens.

plurals
  1. POservice, that listens for Location Service
    primarykey
    data
    text
    <p>So, I was asked to make a Location Tracker. Location tracker should track even if the app is tuned off... My idea is to start my own service (lets call it TrackingService) from the activity by calling startService(intent); so the service will run forever (I guess..) and then connect to Location Service from my own created TrackingService. TrackingService should listen to location changes after app was turned off. I write some code, started TrackingService, and requested location updates in a new thread. Anyway, location updates stops after I quit app but service is still running.</p> <p>EDIT: Ok, so i manage to improve my code a bit, so now when my app is running, i get Log's that my thread (that runs in separate service) is running and that it receives Location Updates. When i quit y app I still get Log that my thread is running but it does not receives Location Updates... Anyone can point my a reason why? P.S. I know that probably there are better ways to get the job done, but I really hoping to fix my code. Here goes service class</p> <pre><code> public class TrackingService extends Service { // DEBUG public final static String TAG = "TrackingService"; public final static boolean D = true; // Global constants private static final long UPDATE_INTERVAL = 10000; // Update frequency in milliseconds private static final long FASTEST_INTERVAL = 4000; // A fast frequency ceiling in milliseconds // int mStartMode; // indicates how to behave if the service is killed private final IBinder mBinder = new LocalBinder(); // interface for clients that bind boolean mAllowRebind; // indicates whether onRebind should be used private int number; // testavimui LocationThread mLocationThread; @Override public void onCreate() { if (D) {Log.d(TAG, "service - onCreated started");}; mLocationThread = new LocationThread(this); mLocationThread.start(); // mLocationThread.run(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (D) {Log.d(TAG, "service - onStartCommand started");}; // The service is starting, due to a call to startService() return mStartMode; } @Override public IBinder onBind(Intent intent) { if (D) {Log.d(TAG, "service - onBind started");}; // A client is binding to the service with bindService() return mBinder; } @Override public boolean onUnbind(Intent intent) { if (D) {Log.d(TAG, "service - onUnBind started");}; // All clients have unbound with unbindService() return mAllowRebind; } @Override public void onRebind(Intent intent) { if (D) {Log.d(TAG, "service - onReBind started");}; // A client is binding to the service with bindService(), // after onUnbind() has already been called } @Override public void onDestroy() { if (D) {Log.d(TAG, "service - onDestroy started");}; // The service is no longer used and is being destroyed mLocationThread.cancel(); } public class LocalBinder extends Binder { TrackingService getService() { // Return this instance of LocalService so clients can call public methods return TrackingService.this; } } public int number(){ number += 1; return number; } private class LocationThread extends Thread implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener{ private boolean keepOn; private Context mContext; private LocationClient mLocationClient; private LocationRequest mLocationRequest; public LocationThread (Context context){ mContext = context; keepOn = true; } public void cancel() { keepOn = false; if (D){Log.d(TAG, "thread was canceled");}; } public void run(){ mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // Use high accuracy mLocationRequest.setInterval(UPDATE_INTERVAL); // Set the update interval to 5 seconds mLocationRequest.setFastestInterval(FASTEST_INTERVAL); // Set the fastest update interval to 1 second mLocationClient = new LocationClient(mContext, this, this); mLocationClient.connect(); while (keepOn){ try { Thread.sleep(10000); if(D){Log.d(TAG, "thread running");}; } catch (Exception e){ } } } @Override public void onConnectionFailed(ConnectionResult result) { if(D){Log.d(TAG, "connection failed");}; } @Override public void onConnected(Bundle connectionHint) { if(D){Log.d(TAG, "connected to location service");}; mLocationClient.requestLocationUpdates(mLocationRequest, this); } @Override public void onDisconnected() { if(D){Log.d(TAG, "disconnected from location service");}; } @Override public void onLocationChanged(Location location) { if(D){Log.d(TAG, "Location changed");}; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload