> If you're going to do that, why involve a handler > at all? Now suppose, I use any other function in my service class instead of handler and say I'm performing a time consuming task in that function. If the onDestroy method of the service is called, I assume that it will terminate my function abruptly. I'm not sure of this, but I thought so and hence used the Handler thread to have a proper shutdown. please correct me if I'm wrong. Also let me know the right way to stop the service.
> Finally, never, ever, call getApplicationContext(). The return value > is not useful to us mortals. Supply the service itself as the context. I'm still not sure what are you saying. How will you supply the service as the context ? Can you provide an example ? On Apr 18, 2:56 am, Bob Kerns <[email protected]> wrote: > Actually, just because something is a background service does NOT mean > it is running in a different thread. > > Background services run in the main thread. I suspect that this point > of confusion may be involved in your problem, though I don't quite > spot the problem. > > Also, you have a handler there that basically loops forever. That's > kind of an oxymoron. If you're going to do that, why involve a handler > at all? If I parse it correctly, it's not the main thread it'll be > blocking, but still... if something in that thread is posting via that > thread's looper rather than the context, that won't be processed. > > Finally, never, ever, call getApplicationContext(). The return value > is not useful to us mortals. Supply the service itself as the context. > To make things maximally confusing, getApplicationContext() will > appear to work in most circumstances. But it's always the wrong way to > get a context to use. > > On Apr 17, 2:05 pm, Tejas <[email protected]> wrote: > > > > > > > Ah... I got this working. Still I'm not sure of the reason for this > > (some thread issue I suppose) It would be great if someone can throw > > some light on this. > > > I was instantiating the sensor class in a background service.So the > > thread in which it was running was different than the main thread. > > I was doing something like this: > > > public class ManagerService extends Service { > > > private final String LTAG = this.getClass().getName(); > > private volatile Looper mServiceLooper; > > private volatile ServiceHandler mServiceHandler; > > > private final class ServiceHandler extends Handler{ > > public ServiceHandler(Looper myLooper) { > > super(myLooper); > > } > > > public void handleMessage(Message msg) { > > Log.v(LTAG, "handleMessage Called"); > > super.handleMessage(msg); > > > // Class Instantiation > > GPSSensor gs = new GPSSensor(); > > gs.setContext(getApplicationContext()); > > gs.startSensing() > > > // Main service loop > > while(CARuntimes.MainServiceRunFlag == true){ > > Log.v(LTAG, "In service Loop"); > > // Do something > > > SystemClock.sleep(60000); > > > }//while > > > } > > } > > > public void onCreate() { > > super.onCreate(); > > HandlerThread myThread = new HandlerThread("Main Service > > Thread"); > > myThread.start(); > > > mServiceLooper = myThread.getLooper(); > > mServiceHandler = new ServiceHandler(mServiceLooper); > > } > > > public void onStart(Intent intent, int startId) { > > super.onStart(intent, startId); > > > Message msg = mServiceHandler.obtainMessage(); > > //msg.obj = blah blah > > mServiceHandler.sendMessage(msg); > > > } > > > public void onDestroy() { > > Log.v(LTAG, "onDestroy called, quitting looper"); > > super.onDestroy(); > > > mServiceLooper.quit(); > > } > > > public IBinder onBind(Intent arg0) { > > return null; > > } > > > } > > > So, I was instantiating the GPSSensor in the handleMessage method. I > > moved this to the onStart method and it started working. > > I'm not sure why it wasn't working earlier and now has started working > > with this change. > > It will be great if anyone can explain this. > > > Cheers, > > Tejas > > > On Apr 17, 1:59 am, Tejas <[email protected]> wrote: > > > > Hi, > > > > My class listed below is not working. No idea whatsoever. The > > > onLocationChanged method is never getting called ! > > > I checked the logs for any errors/exceptions. I have the required > > > permissions set (fine and course locations). > > > I doubt the Context I'm setting from the caller. I'm using > > > getApplicationContext() to pass to the setContext method in this > > > class. > > > Can anyone please help ? > > > > public class GPSSensor implements CASensor { > > > > private String LTAG = this.getClass().getName(); > > > Context myContext; > > > private String GPSData; > > > public LocationManager lMgr; > > > public LocationListener myLocListener = new LocationListener() { > > > > public void onStatusChanged(String provider, int status, > > > Bundle > > > extras) { > > > Log.v(LTAG, "=========== Here 1 ============"); > > > } > > > > public void onProviderEnabled(String provider) { > > > Log.v(LTAG, "=========== Here 2 ============"); > > > } > > > > public void onProviderDisabled(String provider) { > > > Log.v(LTAG, "=========== Here 3 ============"); > > > } > > > > public void onLocationChanged(Location location) { > > > if (location != null){ > > > GPSData = location.getLatitude() + "," + > > > location.getLongitude(); > > > Log.v(LTAG, "GPS data received > > > ===========> " + GPSData); > > > } > > > else > > > Log.v(LTAG, "Location is null > > > ===========> "); > > > } > > > }; > > > > public String getCurrentData() { > > > return GPSData; > > > } > > > > public void setContext(Context context) { > > > myContext = context; > > > } > > > > public boolean startSensing() { > > > if (myContext == null){ > > > Log.w(LTAG, "myContext not set"); > > > return false; > > > } > > > Log.v(LTAG, "Starting location updates !"); > > > > lMgr = (LocationManager) > > > myContext.getSystemService(Context.LOCATION_SERVICE); > > > lMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, > > > 0, 0, > > > myLocListener); > > > > > > //lMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, > > > 0, myLocListener); > > > Log.v(LTAG, "Registered myLocListener for GPS"); > > > return true; > > > } > > > > public boolean stopSensing() { > > > if (myContext == null){ > > > Log.w(LTAG, "myContext not set"); > > > return false; > > > } > > > Log.v(LTAG, "Stopping location updates !"); > > > lMgr.removeUpdates(myLocListener); > > > return false; > > > } > > > > }//GPSSensor > > > > -- > > > You received this message because you are subscribed to the Google > > > Groups "Android Developers" group. > > > To post to this group, send email to [email protected] > > > To unsubscribe from this group, send email to > > > [email protected] > > > For more options, visit this group > > > athttp://groups.google.com/group/android-developers?hl=en > > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group > > athttp://groups.google.com/group/android-developers?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group > athttp://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

