The CommonsBlog

Presentations From a Service

For a while now, I have been saying that the only way to use the Presentation class to display stuff on a TV/monitor/projector is through an Activity, because Presentation inherits from Dialog. That is true.

Sometimes, I would say that the only way to display stuff on a TV/monitor/projector is through an Activity. That, as it turns out, is not true.

One Stack Overflow answer led to my own Stack Overflow question on the subject, and with the help of those SO contributors I eventually figured out how to drive an external display without an Activity.

I wrapped that pattern up into a PresentationService that I have added to my CWAC-Presentation library. PresentationService clones some of the logic from Presentation and Dialog, enough to allow you to define a View that will be shown on a Window connected to the Display object representing the external display.

The biggest limitation is that we have to use a TYPE_SYSTEM_ALERT Window, which in turn requires the SYSTEM_ALERT_WINDOW permission. This is a permission that I really try to avoid, both as a developer and as a user. Ostensibly, it is there because otherwise a Service could draw over the main display without the user being aware of it. Perhaps in the future, a Service rendering to an external display might be able to avoid this permission or switch to some alternate, less-scary permission.

I hope to cover the implementation of PresentationService in the next update to my book. If you simply want to use it, the project README contains basic instructions for using it, and there is a dedicated sample project showing how to use it.

Do you need a book on Android app development, but are having trouble finding one that is new enough? Try The Busy Coder’s Guide to Android Development!