Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a View from a Service?

Tags:

android

Already asked a similar question, yet without much luck.

Suppose I have a service and I need a view to pop up above it. In the same time, they both should be intractable, i.e. the user should be able to both click buttons within the view, as well as ones on the service in the background.

enter image description here

Is this in theory possible? If yes, how should I initialize that view?

Thanks!

like image 860
Roger Avatar asked Sep 22 '11 20:09

Roger


4 Answers

Yes it's possible, what you need to do is call the WindowManager service and add your view via the same.

WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.box,null);

You need a WindowManager.LayoutParams object which should contain the parameters for the layout

windowManager.addView(layout,params);

Well, adds the view

like image 131
000 Avatar answered Nov 01 '22 03:11

000


What you want is to add a view from your running service instance. This way you can persist the view across all activities - and from anywhere else. See this great example:

http://www.piwai.info/chatheads-basics/

like image 44
slott Avatar answered Nov 01 '22 03:11

slott


Services most definitely can have a user interface: Input methods are an obvious example. See http://developer.android.com/resources/samples/SoftKeyboard/index.html for an example.

like image 32
Aharon Manne Avatar answered Nov 01 '22 01:11

Aharon Manne


I guess you are misusing the word "Service". Service is invisible, Activities are visible.

There are no buttons in an Service!

So you have no choice! You should put both views in one Activity, and I would use a RelativeLayout and set the visibility of your chidren to GONE/Visible.

http://developer.android.com/reference/android/widget/RelativeLayout.html

Also using a popup and making the layout under it clickable will disturb the user. You are completely changing User experience. I strongly suggest too make your popup appear at the top/bottom of your initial layout

like image 35
Waza_Be Avatar answered Nov 01 '22 02:11

Waza_Be