简体   繁体   English

如何在Android应用程序的任何活动中显示警报对话框?

[英]How to display alert dialog in any activity in android application?

In my application I want to display common AlertDialog for all activities. 在我的应用程序中,我想为所有活动显示常见的AlertDialog
In my background thread, Server data is coming periodically. 在我的后台线程中,服务器数据会定期出现。

Now if some user defined criteria are matched with that data, I want to display AlertDialog on screen in any activity which is currently on front. 现在,如果某些用户定义的条件与该数据匹配,我想在屏幕上显示当前正在进行的任何活动中的AlertDialog

How can I identify that where to display AlertDialog ? 如何识别AlertDialog显示AlertDialog
And if my application is in background, I want to set Notifications rather than AlertDialog . 如果我的应用程序在后台,我想设置Notifications而不是AlertDialog

You will need a Service running in background. 您将需要在后台运行的服务。

All the needed code is there: 所有需要的代码都在那里:

http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/ http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/

Your service will send a broadcast to your Activity. 您的服务将向您的活动发送广播。 If your activity is not in foreground, nothing will happen. 如果您的活动不在前台,则不会发生任何事情。

You can show alert dialog from background by any one of below... 您可以通过以下任何一个从后台显示警报对话框...

But before your show dialog you need to check if activity is running or not by using one boolean flag as below otherwise you will get WindowManager$BadTokenException . 但是在你的show对话框之前,你需要通过使用一个布尔标志检查活动是否正在运行,否则你将获得WindowManager$BadTokenException

// Flag to check if activity is running or not
boolean isActivityRunning = false;

@Override
protected void onResume() {
    super.onResume();
    isActivityRunning = true;
}

@Override
protected void onPause() {
    super.onPause();
    isActivityRunning = false;
}

1. runOnUiThread 1. runOnUiThread

You need to show your dialog on UI thread like below... 您需要在UI线程上显示您的对话框,如下所示...

runOnUiThread(new Runnable() {
            @Override
            public void run() {
               if(isActivityRunning) {
                 // Your dialog code.
                 AlertDialog ActiveCallDialog = new AlertDialog.Builder(context)
                    .setMessage(R.string.message)                       
                    .setTitle(R.string.title)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
               }    
            }
        });

2. Handler 2.处理程序

You can create an handler in Activity Class, and can invoke sendMessage to that handler object. 您可以在Activity Class中创建一个处理程序,并可以将sendMessage调用到该处理程序对象。 Write code to display alert in handleMessage method of Handler, for Example: 编写代码以在Handler的handleMessage方法中显示警报,例如:

Activity Class 活动类

Handler mHandler = new Handler()
{
    public void handleMessage(Message msg)
    {
       if(isActivityRunning) {
          //Display Alert
          AlertDialog ActiveCallDialog = new AlertDialog.Builder(context)
                    .setMessage(R.string.message)                       
                    .setTitle(R.string.title)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();             
        }            
    }
};

Thread 线

Thread thread= new Thread()
{
    public void run()
    {             
         mHandler.sendEmptyMessage(0);
    }
}

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

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