0

I have been trying to develop Location listener using NMEANListener in android.

I have a main activity which has start button. Clicking on start button initializes the locationManager and NMEAListener. After this I get notifications in

public void onNmeaReceived(long arg0, String arg1)

However after some time my NMEAListener is removed by android.

My Mainactivity.java class has following instances declared

private LocationManager locManager  =   null;
private TextView output     =   null;
private NMEAMessageListener nmeaMessageListener     =   null;

I initialize the listener in Main activity class on click of button

nmeaMessageListener =   new NMEAMessageListener();


locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000,100,nmeaMessageListener);


Location location=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Please let me know as the reason for Listener being removed by android. Is there a better way of doing it. I want to keep my location listener running for long time. Shall i use intentservice to start NMEANListener.

Any help will be appreciated.

user3301213
  • 41
  • 2
  • 7

1 Answers1

0

If you want to listen for location changes while your app is in background, then put your location listener inside Service. The fact that your listener is removed, is becaus Android is more likely to kill your app while it is in background. If you really want your service to survive low memory conditon, then add notification to is and make if foreground (call setForeground).

IntentService is more suited for single but longer tasks, it provides you with function that will perform task you want to perform in background and after it finished service will probably be finished. This makes it not appropriate for the location listener.

If location listener is implemented in Activity, then you probably remove it yourself inside onStop which is called once Activity is hidden.

Here is example SO: Background service with location listener in android

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100