Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

android - Display AlertDialog as system overlay window from Service

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.

Scenario:

  • Running Service. No active Activity present.
  • On some external event, Service sends Notification
  • When user press Notification, Service is informed via PendingIntent and AlertDialog should be displayed (created with AlertDialog.Builder(this))

Error:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution that matches described scenario has been found.

  • 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:style/Theme.Translucent

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

android:theme="@android:style/Theme.Translucent"

  • In onCreate() actual display of AlertDialog occurs

  • 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.

  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...