Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable hide of a Presentation (Secondary Screen)

I´m working on a project which should show a little presentation on a secondary display (API 4.2). Till now all is working fine. But if I close the application I can´t prevent presentation from being closed. I would like to create a application which is starting a background service which is initializing the presentation. So that the user just see a notification icon in the statusbar but the service is streaming the content to the secondary display.

Any ideas if this could be realized? I tried it with the backgroundservice but don´t know which context I should use. Tried it with

getApplicationContext()

but then I just get a exception:

Unable to add window -- token null is not for an application

If I use a static method like getAppContext() (I know such a ugly hack) it will show the presentation but will also hide it if i close the application.

Any ideas?

like image 529
mkl Avatar asked Apr 22 '13 13:04

mkl


2 Answers

Create a Service that uses the Context.createDisplayContext(Display) to obtain a new Window manager for your secondary display - this is how Presentation works

take a look at this link as an example: http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/com/android/server/LoadAverageService.java.htm

In onCreate() instead of getting the WindowManagerImpl:

public void onCreate(){
    ... 
    WindowManagerImpl wm = (WindowManagerImpl)getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params); 
}

call a method like this:

private void addView(WindowManager.LayoutParams params){
    DisplayManager dm = (DisplayManager) getApplicationContext().getSystemService(DISPLAY_SERVICE);
    if (dm != null){
        Display dispArray[] = dm.getDisplays(); 

        if (dispArray.length>0){
            Context displayContext = getApplicationContext().createDisplayContext(dispArray[1]);
            WindowManager wm = (WindowManager)displayContext.getSystemService(WINDOW_SERVICE);
            wm.addView(mView, params);
        }
    }
}

Your view will be added to the secondary display and since this runs by a Service, your activity running on the main display is not paused

like image 74
icarmel Avatar answered Oct 13 '22 16:10

icarmel


Any ideas if this could be realized?

Since Presentation is a subclass of Dialog, your Presentation can only be visible when you have the activity hosting it in the foreground.


UPDATE

icarmel's answer works, though it is missing some details. I have a PresentationService now in my CWAC-Presentation library that offers a complete working implementation of the technique. To answer my question in my comment on icarmel's answer, you use WindowManager.LayoutParams.TYPE_SYSTEM_ALERT in the WindowManager.LayoutParams, which in turn requires the SYSTEM_ALERT_WINDOW permission (unfortunately).

like image 23
CommonsWare Avatar answered Oct 13 '22 16:10

CommonsWare