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 at
http://groups.google.com/group/android-developers?hl=en