Android live wallpaper service not getting destroyed
I have created a Live Wallpaper which takes a file chosen by the user to be the background image.
The problem I'm having is that when I click 'Set Wallpaper' it begins my wallpaper service, then if I go back into my live wallpaper and click 'Set Wallpaper' again, it doesn't seem to close the previous service, but just runs another as well. This means each time I click 'Set Wallpaper' an image from the users SD card is read into a Bitmap variable and increases the memory used each time, which eventually throws an OutOfMemoryError.
My onDestroy() method开发者_开发知识库 nullifies all Bitmap references and does System.gc(), however in this case, it seems the service isn't being destroyed when setting the same wallpaper over it.
For example, if I had my wallpaper set, then chose to set the 'Galaxy' live wallpaper, this would destroy my wallpapers service, but when setting my wallpaper again, it does not destroy the original service.
Has anyone come across this before? Is there a different method I should be nullifying and garbage collecting in? Any help would be much appreciated.
Before setting wallpaper again. Destroy the previous wallpaper. This worked for me. And Destroy Wallpaper in Activity from which you are calling WallpaperService. Destroying inside WallpaperService results in default Wallpaper of device.
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.clear();
} catch (IOException e) {
e.printStackTrace();
}
Hope this helps.
I'm pretty sure I've figured it out for anyone else having this problem.
It seems that when I already have my wallpaper running, then I click 'Set wallpaper' on its preview, the onDestroy() method isn't called. However, nullifying Bitmaps and calling stopSelf() can be put in the onSurfaceDestroyed method, this seems to work for each instance of the WallpaperService Engine open. It might not be the correct way to do things but it seems to work ok at the moment.
Thanks to GeekYouUp for help with stopSelf().
Is the service calling stopSelf() once it is finished?
精彩评论