3

I think the question says it all: Is it possible to set the window parameters of the current Activity from a service? So in an Activity you have the Method getWindow() with which it is possible to change the window FLAGs from the Activity. Now I want to call this Method from a Service:

public class WindowService extends Service
{
    public WindowService()
    {
        // get current activity
        activity.getWindow().addFlag(FLAG);
    }

    // other stuff
}
a.ch.
  • 8,285
  • 5
  • 40
  • 53
Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • 4
    Can't you simply broadcast a message from the `Service` to the `Activity` and let it change the `Window` on its own? – user Jan 13 '14 at 07:06
  • http://stackoverflow.com/a/4753333 this shows how to get current activity via ActivityManager – Palejandro Jan 13 '14 at 07:15
  • @Plejandro I found this code too but I think with it I can't get a real Activity object. So I don't know how to call getWindow from the activity. To Ago that is no good solution for my problem but I can't tell you more about that. – Cilenco Jan 13 '14 at 11:09

3 Answers3

1

You should not mix the service and activity. The service should not do any UI. Instead as others suggested, the service should send a message to the activity and the activity may change it's UI. Be ready for cases where the service is running but the activity is not. With broadcast it's simple, you send a message but no-one gets it, no need to test if the activity is running.

yoah
  • 7,180
  • 2
  • 30
  • 30
0

Since you're not operating within an activity context, you need a handle to one. Either use a broadcast receiver to send a message to your listening activity, which is the right way to handle it, or pass a reference to the activity, like this:

void WindowService(Activity activityReference) {
    activityReference.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

I would recommend using a BroadcastReceiver though.

Mike P.
  • 1,920
  • 1
  • 18
  • 20
0

You can do it also without any Broadcast, you can bind the Activity with the Service to communicate back & forth(using any of the three mechanisms available - implementing IBinder, using Messenger or implementing AIDL), but one thing you should remember whatever you want to do with activity's window, you vahe to do within the scope of Activity's Context & that also from UI thread.

CR Sardar
  • 921
  • 2
  • 17
  • 32