Search code examples
androidservicepresentation

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?


Solution

  • 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