简体   繁体   English

在任何应用程序活动中,Android从后台线程显示对话框

[英]Android show dialog from background thread in any activity of application

I tried to look clear answer for it but wasn't able to find it anywhere. 我试图为它找到清晰的答案,但是在任何地方都找不到。 I am running background thread in main activity that checks for certain variable and if it is true it should show alert dialog. 我在主要活动中运行后台线程,该线程检查某些变量,如果该变量为true,则应显示警报对话框。 I also want this dialog to show up on any focused activity of the application. 我还希望该对话框显示在应用程序的所有重点活动上。 I tried it by adding Looper.prepare() and Looper.loop() to the thread but it does not work properly and it affects while() loop that I use to check variable in that thread. 我通过将Looper.prepare()和Looper.loop()添加到线程中进行了尝试,但是它无法正常工作,并且会影响我用来检查该线程中变量的while()循环。 Can anyone please help me in finding out what is the best way to implement this? 谁能帮我找出实现此目标的最佳方法是什么?

Thanks. 谢谢。

If you construct the background thread using the main/ui thread, you can create a Handler in the constructor. 如果使用main / ui线程构造后台线程,则可以在构造函数中创建Handler When you want to run some code on the main/ui thread, you simply Handler.post(Runnable r) with a runnable to the ui thread. 当您想在主/ ui线程上运行某些代码时,只需将Handler.post(Runnable r)与可运行到ui线程即可。

If your background thread is not being constructed on the ui/main thread, you can use a BroadcastIntent to and a BroadcastReceiver pattern to send messages between your background thread and your foreground activities. 如果后台线程不是在ui / main线程上构造的,则可以使用BroadcastIntent到和BroadcastReceiver模式在后台线程和前台活动之间发送消息。 This is especially useful if you are switching foreground activities during the useful life of your background thread. 如果您在后台线程的有效期内切换前台活动,这将特别有用。

Ok, i can see two approaches. 好的,我可以看到两种方法。 The first one is a dirty but quick: You can extend TimerTask and Handler classes. 第一个是肮脏但快速的:您可以扩展TimerTaskHandler类。 YourTimerTask will check variable and send a Message to YourHandler . YourTimerTask将检查变量并将Message发送到YourHandler YourHandler should override handleMessage and show a dialog. YourHandler应该覆盖handleMessage并显示一个对话框。

The second one might be an overkill, but still. 第二个可能是一个矫kill过正,但仍然如此。 Android is event-based . Android是基于事件的 It means that system gives you an opportunity to create your own events and handle it. 这意味着该系统使您有机会创建自己的事件并进行处理。 So, you can start a Service , which will check your variable and send a Broadcast (can be local). 因此,您可以启动Service ,它将检查您的变量并发送Broadcast (可以是本地的)。 In your activity you have to create your own BroadcastReceiver and register it. 在您的活动中,您必须创建自己的BroadcastReceiver并进行注册。 This receiver will handle a message. 该接收者将处理一条消息。

You may want to try creating an implementation of Runnable and passing that to a View 's post() method. 您可能想要尝试创建Runnable的实现,并将其传递给Viewpost()方法。

final Runnable r = new Runnable() {
    public void run() {
        //code to display dialog
    }
}

final View view = findViewById(R.id.XYZ);
view.post(r);

This will run the Runnable on the UI thread. 这将在UI线程上运行Runnable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM