如何在android上当前可见的活动上显示对话框? [英] How can I display a dialog on Currently visible activity on android?

查看:99
本文介绍了如何在android上当前可见的活动上显示对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于

My Problem is similar to this 2 year old question, I am just re posting the same problem to get the updated answers, since a lot has changed in two years.

我正在为GingerBread +设备开发应用程序,我从事很多活动,并且在后台我从服务器接收到一些数据.现在在某些情况下基于该数据,我需要向用户显示Dialog.问题是我怎么知道当前最活跃的活动?

I am developing for an app for GingerBread+ devices, I have many activities and in background I receive some data from the server. Now based on that data in some cases I need to show a Dialog to the user. Problem is How do I know which the current front most activity ?

我尝试过的事情 我尝试在创建Dialog的同时提供getApplicationContext(),但是那是行不通的.抛出一些异常.

What I tried, I have tried giving the getApplicationContext() while Dialog creation, however that is not working. Throwing some exception.

解决方案? (我真的很讨厌), 一个解决方案可能是通过在Application类中具有一个变量并将其设置在每个活动的onResume()上来跟踪当前可见的活动.如果他们是实现此目标的更聪明的方法,我真的不想保留这本书,我相信他们是实现此目标的更明智的方法,

A solution ? (I really hate it), A solution could be to keep track of the currently visible activity by having a variable in Application class, and setting it on onResume() of each activity. I really don't want to do this book keeping if their are smarter ways to achieve this and I am sure their are smarter ways to achieve this,

我的简单问题是,
如何在当前可见的活动"上显示对话框?,以便可以将其引用到AlertDialog.Builder,我认为它将完成我的工作.在最上方的活动上显示对话框?

My simple question is,
How can I display a dialog on Currently visible activity ?, So that I can give that reference to the AlertDialog.Builder, which I think will do my job.. If not than How I can display a dialog on topmost Activity ?

编辑,我使用以下代码创建一个简单的对话框 私有View.OnClickListener cancelClickListener = new OnClickListener(){

Edit, I create a simple dialog using following code private View.OnClickListener cancelClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                LoginActivity.this);

        // set title
        alertDialogBuilder.setTitle("Roobroo will exit..");

        // set dialog message
        alertDialogBuilder
                .setMessage("Are you sure you want to exit ?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // if this button is clicked, close
                                // current activity
                                LoginActivity.this.finish();
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
        // TODO Write the code to exit from the app, (gracefull exit)
        Log.i(LOG_CAT, "Cancel Button is clicked");
    }
};

例外 使用AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder( getApplicationContext());给了我以下例外,

Exception using AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getApplicationContext()); gives me following exception,

06-11 14:09:16.732: E/AndroidRuntime(1005): FATAL EXCEPTION: main
06-11 14:09:16.732: E/AndroidRuntime(1005): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.view.ViewRoot.setView(ViewRoot.java:531)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.app.Dialog.show(Dialog.java:241)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at com.mycompany.myapp.activities.LoginActivity$3.onClick(LoginActivity.java:127)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.view.View.performClick(View.java:2485)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.view.View$PerformClick.run(View.java:9080)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.os.Handler.handleCallback(Handler.java:587)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.os.Looper.loop(Looper.java:123)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at java.lang.reflect.Method.invoke(Method.java:507)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 14:09:16.732: E/AndroidRuntime(1005):     at dalvik.system.NativeStart.main(Native Method)

推荐答案

如果有帮助,请尝试以下操作:

Try this if it helps you:

1..用transparent theme and no title创建一个Activity.

2..在onCreate()中定义您的alert dialog.

3..从broadcastReceiver开始此活动将显示alert dialog.

3. Starting this activity from broadcastReceiver will show the alert dialog.

这篇关于如何在android上当前可见的活动上显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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