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

时间:2016-05-27 12:10:30

标签: android

我想显示一个警告对话框。此警报对话框在一个活动中创建。但问题是,当我从服务器

获得一些响应时,我想在应用程序中的任何活动之上显示此对话框

2 个答案:

答案 0 :(得分:3)

是的,我得到了解决方案......

首先我创建了一个用于显示警告对话框的类。 收到服务器的响应,调用方法通过传递应用程序上下文来显示警告对话框

package io.omoto.omotokairaliapp.Utls;

import android.content.Context;
import android.view.WindowManager;

import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;

import io.omoto.omotokairaliapp.Constants.Constants;

/**
 * Created by ${venkie} on ${28/1/16}.
 */
public class DisplayRegisteredMessage {

    Context context;
    private NiftyDialogBuilder dialogBuilder;


    public DisplayRegisteredMessage(Context context) {

        this.context = context;
    }

    public void displayMessage() {

        if (Constants.FLAG == 1) {
            Constants.FLAG = 0;

            dialogBuilder = NiftyDialogBuilder.getInstance(context);
            dialogBuilder
                    .withTitle("Response is already registered")
                    .withMessage("For this Customer we have already taken response Sorry!!");
            dialogBuilder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialogBuilder.show();

        } else {


        }
    }


}

以上代码显示警告对话框

@Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("response page", response.toString());
                try {
                    if (response.getString(Constants.STATUS).equalsIgnoreCase("SUCCESS"))
                        p7.deleterecord(p7b.getFlowid());
                        p7.deleteRows();

                    Constants.FLAG = 1;
                    DisplayRegisteredMessage displayRegisteredMessage = new DisplayRegisteredMessage(mContext);
                    displayRegisteredMessage.displayMessage();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("Page 6", p7b.getFlowid());
            }

答案 1 :(得分:0)

在其他类(非活动)中创建警告对话框,如下面的静态方法&中的代码所示。使用UDF.showAlertDialog(activity)调用此方法,以显示对话框。

public class UDF { 
public static void showAlertDialog(
                String warning,
                DialogInterface.OnClickListener positiveClickListener,
                DialogInterface.OnClickListener negativeClickListener,
                Context context) {

            AlertDialog.Builder alertdialog = new AlertDialog.Builder(context);
            alertdialog.setMessage(warning);

            alertdialog.setPositiveButton("Ok", positiveClickListener);
            alertdialog.setNegativeButton("Cancel", negativeClickListener);
            alertdialog.show();

        }

    }

编辑:设置此

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

并在menifest.xml中添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

&安培;在创建活动或上下文的对话框实例时传递getApplicationContext()