0

So, I have to create few items, like Buttons and TextViews, from a service.

Since I need to pass along a Context when I create a new one, and since my service doesn't have one, I thought about doing it like this

Button button = new Button(getApplicationContext());

It works, but is it the best solution? I read a lot about how you have to be extra careful when using application context, so I'm not sure if it's the proper way to achieve what I need

Thanks in advance

Shark107
  • 27
  • 4
  • 4
    Why do you need buttons and textviews in a `Service` ? – Shaishav Oct 03 '16 at 09:48
  • I have an Overlay Service which, as you can guess by the name, it puts on screen buttons and views when and how I need them, even when I switch between different applications - it has to be an overlay on top of different applications, so that's why. Can you help me or it was just curiosity? :) – Shark107 Oct 03 '16 at 09:56
  • here is how you can acheive it - http://icetea09.com/blog/2015/03/16/android-floating-view-like-facebook-chatheads/ – Lalit Poptani Oct 03 '16 at 13:34

1 Answers1

1

I never used Service instances for that so, this is new for me. However, I stumbled upon this question about overlay service which gives quite cool solutions. As per your question I guess the following should work within the scope of your Service and is safer:

Button myButton = new Button(this);
Community
  • 1
  • 1
Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • Thanks, I already managed to achieve that overlay, and my solution is pretty similar :) I didn't realize I could have just used _this_ , that's why I was searching for other solutions! Thanks a lot, I'll see if it's stable this way – Shark107 Oct 03 '16 at 14:16