显示AlertDialog从服务体系覆盖窗口 [英] Display AlertDialog as system overlay window from Service

查看:102
本文介绍了显示AlertDialog从服务体系覆盖窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从服务显示AlertDialog问题。我能够显示使用吐司或使用窗口管理器(TYPE_SYSTEM_ALERT或TYPE_SYSTEM_OVERLAY)自定义布局窗口。但是,我不希望使用自定义布局,我preFER直接使用漂亮的AlertDialog图形用户界面。

I have issue with displaying AlertDialog from Service. I am able to display custom layout window using Toast or using WindowManager(TYPE_SYSTEM_ALERT or TYPE_SYSTEM_OVERLAY). But, I do not want to use custom layout, I prefer to use nice AlertDialog GUI directly.

方案:

  • 正在运行的服务。没有活动活动present。
  • 在一些外部事件,服务发送通知
  • 当用户preSS通知,服务是通过PendingIntent和AlertDialog告知应显示(用 AlertDialog.Builder创建(本)

错误:

ERROR/AndroidRuntime(1063): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

在搜索的答案使我即时通讯pression,我想的东西是现在无法接受(安卓2.2)。或者,也许它是。

Searching for the answer lead me to impression that I am trying something that is currently not possible (Android 2.2). Or maybe it is.

推荐答案

解决方案将满足描述的场景已经找到。

Solution that matches described scenario has been found.

  • 新活动已创建并从服务开始。然而,它是半透明的背景活性。这种活动不具有的onCreate行 super.setContentView()()。更重要的是,要确保透明度

  • New Activity has been created and started from Service. However, it is activity with translucent background. Such activity does not have line super.setContentView() in onCreate(). More important, to ensure transparency

@android:款式/ Theme.Translucent

@android:style/Theme.Translucent

根据主题标签进入在AndroidManifest.xml中GUI这项活动。因此,新行添加到清单XML是

is entered under Theme tag for this activity in AndroidManifest.xml GUI. So, new line added to the manifest xml is

安卓主题=@安卓风格/ Theme.Translucent

  • 的onCreate() AlertDialog的实际显示发生

  • In onCreate() actual display of AlertDialog occurs

在AlertDialog按钮preSS使对话和活动结束,并发送意向书(或使用其他方式),实现结果的服务。

Press on AlertDialog buttons causes Dialog and Activity closing and sending of the Intent (or use of some other means) to deliver result to the Service.

请确保您定义setOnDismissListener()的对话(实现应该呼吁活动结束())。如果你不这样做,preSS上的后退键取消对话框,而是让你在目前的活动是透明的,看起来用户喜欢的事,是真的错了。

Be sure that you defined setOnDismissListener() for the Dialog (implementation should call finish() on activity). If you do not do that press on the Back key cancels dialog but leaves you in the current activity which is transparent and looks to user like something is really wrong.

这篇关于显示AlertDialog从服务体系覆盖窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆