tags:

views:

36

answers:

1

I am still working on my location-based alarm android application. I have an AlarmService class that starts notifications and proximity alerts. I am starting this service with:

startService(intentAlarmService);

I try to stop the service using:

Intent intentAlarmService = new Intent(this, AlarmService.class);
stopService(intentAlarmService);

This is what happens: The service stops, but then, when I start another instance of the service (i.e. exit the app, launch the app, start the service) - I discover (through Toasts) that the previous instances of the service are still running. For example, in the AlarmService class, there is a LocationListener with the onLocationChanged method. So, in this method, I put:

Toast.makeText(AlarmService.this, "AlarmTitle: " + mAlarmTitle, Toast.LENGTH_SHORT).show();

And when I re-start the service, Toasts keep showing up with the previous AlarmTitles, and the current AlarmTitle.

So, something is not working when I try to stop the AlarmService - what could this be?

Note: when I re-install the application, the service stops for real. Then when I start the service, only the current AlarmTitle shows in the Toast (I want this to happen every time).

Something is wrong with my service. Any ideas what I can do?

thanks.


CODE FROM MY APP:

public void onDestroy(){
super.onDestroy();

Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);

PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);

pendingIntentAlarm.cancel();

Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class);

stopService(intentAlarmService); 

mNtf.cancel(NOTIFICATION_ID1);
mNtf.cancelAll();  

}

+1  A: 

I discover (through Toasts) that the previous instances of the service are still running.

My guess is that you are leaking services, probably by failing to call removeUpdates() to detach your LocationListener. There is only one true running copy of the service (from an Android lifecycle standpoint), but you are preventing the other services from being garbage collected through your leak.

Also, please replace all occurrences of getApplicationContext() with this.

CommonsWare
I'm still a little new to Android programming. I know what memory leaks are, but what is meant by "leaking services" ? Can you please offer some more explanation in terms of what seems to be wrong with my app? Thanks, and I'm sorry about the occurrences of getApplicationContext().
Yasir Malang
@Yasir Malang: I believe you are calling `registerLocationUpdates()` and not calling `removeUpdates()`.
CommonsWare
Thank you for your help. I have looked through my code and I found 3 separate implementations of the LocationListener class. I did this because I do not know how to access 1 public class from another class. How would you write code to make 1 public LocationListener class and then have other classes inherit from that?
Yasir Malang