3

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
William Stewart
  • 841
  • 1
  • 14
  • 27

3 Answers3

4

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.

viveksb007
  • 61
  • 5
  • this will clear your wallpaper as well when you come back from preview screen without setting any wallpaper – waseem Jun 24 '21 at 06:34
1

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().

William Stewart
  • 841
  • 1
  • 14
  • 27
0

Is the service calling stopSelf() once it is finished?

GeekYouUp
  • 1,651
  • 11
  • 10
  • No its not, should that be in the onDestroy() method? – William Stewart May 25 '11 at 16:28
  • I tried calling stopSelf() in the onDestroy() method but nothings changed. To test it out I set the phone to vibrate inside the onDestroy() method, which works when I exit the wallpaper preview part where I can change settings, etc. But it doesn't seem to do it when setting my wallpaper over the same wallpaper. Its like it doesn't call onDestroy unless I'm setting a new wallpaper. – William Stewart May 25 '11 at 16:43
  • No, it should be called once the service has finished doing what you started it for. Have a read of this http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle – GeekYouUp May 25 '11 at 17:09
  • Sorry if its obvious but I'm slightly confused. The wallpaper will continue to run until the user chooses another wallpaper, so isn't the point that its finished when the onDestroy method is called? Again, apologies if this is something simple but I'm relatively new to the Android development scene and this is the only problem I haven't been able to fix myself. – William Stewart May 25 '11 at 17:35
  • I've run some tests on a live wallpaper I created and the WallpaperService onCreate() is only getting called once, no matter how many times I reapply it, then onDestory() is also only getting called once, when I apply a different wallpaper. So I understand your problem better now, but I'm not sure what is causing it. Here is the code http://j.mp/lNzGK1 , I added Lod.d() lines into the onCreate and onDestory to debug. Any help? – GeekYouUp May 26 '11 at 06:28
  • Yes unfortunately that's proved what I thought, I've done some reading and it seems multiple instances of the WallpaperServices can be run, each being represented by its own Engine, so I assume its not closing the previous service and initiating a new one. At the moment, it seems that its just the way its coded by Android. I've looked at some similar live wallpapers on the market that have high ratings and lots of downloads, and they seem to have the same problem, although no one has highlighted it, so I can only assume its not that big of a deal. Thanks for the help GeekYouUp. – William Stewart May 26 '11 at 10:28
  • Has anyone got any more info on this topic. The only problem at the moment is Bitmaps, when multiple instances of the wallpaper are run and when its a 3MB photo from their SD card it really starts filling up the 16MB allowance fast. – William Stewart May 28 '11 at 18:50