How to configure a window on an external display from a service?

Android added Presentation to API Level 17 (Android 4.2) to support displaying content on an external Display , such as a TV or monitor connected via HDMI, MHL, Miracast, or SlimPort. However, Presentation extends Dialog , and therefore it can only be used from Activity .

And, as far as I know, this was the end of the story.

However, https://stackoverflow.com/a/16626832/2326328# However, it hints at a possible way to use an external Display from Service using createDisplayContext() and WindowManager created from this Context . Then the addView() method on this WindowManager should display the View on the specified Display . If it can be made to work, it really opens the door to an interesting use of external displays, such as playing video on a TV, when you can use unrelated applications (such as a web browser) on the device’s own touch screen.

However, this answer ignores a key detail: how to configure the call to WindowManager.LayoutParams to call addView() . In particular, there is a dizzying array of possible TYPE_ values ​​for the type field. I crashed in two attempts, albeit with different messages:

  • TYPE_APPLICATION leads to android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

  • TYPE_APPLICATION_MEDIA leads to android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

For example, here is my WindowManager.LayoutParams for the second scenario above:

 WindowManager.LayoutParams p= new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, 0, 0, WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA, 0, PixelFormat.OPAQUE); 

Reading through documents for type suggests that none of the TYPE_APPLICATION will be correct, since I don't have a token . Moreover, the point behind this exercise is to not have a token as much as possible, since the Service supposed to run independently of any user interface.

If you look at the source on Presentation , it will cancel WindowManager on Dialog , which uses com.android.internal.policy.PolicyManager , which quickly ends on IPolicy . In any case, the SDK application does not have access to the PolicyManager .

Has anyone got the createDisplayContext() approach to work from Service ? If so, what did you use for type (or, moreover, for WindowManager.LayoutParams in general)? Reward points for a decision that does not require any illegal permission. :-)

Thanks!

+7
android
source share
1 answer

The TYPE_SYSTEM_ALERT type, used connectively with SYSTEM_ALERT_WINDOW permission, should work.

It makes sense to start a dialog from a service that requires icky permissions, basically it allows you to draw other applications :)

+6
source share

All Articles