How to show alert dialogs at any time on a high-frequency live card?

喜夏-厌秋 提交于 2019-12-25 03:38:07

问题


I'm working on Glassware that needs to be able to display alert dialogs at any time during the lifespan of the Glassware. As can be seen on Google's GDK development guide, this requires a valid Context object. However, I'm using the high-frequency live card design paradigm, and thus it uses a Service (rather than an Activity) as its main class. If I try to pass the service as the context to the alert dialog constructor, I get a BadTokenException.

Is there a recommended way to show alert dialogs at any time with this type of Glassware? I have been able to show them successfully from the Glassware menu, since the menu is indeed an activity with a usable context object, but I need to be able to show alerts when the menu is closed, too.

For the record, I have been able to find a couple of possible solutions that seem pretty hacky: create a separate dialog-themed activity, or use a system alert, as seen in the top two posts here. I'd rather avoid these if there's a better way to do it.

Edit: I've accepted the one answer given so far, but if anyone does know of a more "official" way to do it, please post a second answer and I'll probably accept it instead.


回答1:


Create a CustomAlertDialogBuilder that extends AlertDialog.Builder and use the service as the context.

Once you have your alert dialog however I don't believe you can avoid:

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 


来源:https://stackoverflow.com/questions/26238445/how-to-show-alert-dialogs-at-any-time-on-a-high-frequency-live-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!