如何从另一个类显示alertdialog并在当前活动(Android)中处理它

如何从另一个类显示alertdialog并在当前活动(Android)中处理它,android,android-alertdialog,android-notifications,Android,Android Alertdialog,Android Notifications,我在某些情况下发送通知,如果应用程序位于后台,则通过通知通知用户;如果用户位于前台,则通过警报通知用户。现在我想知道如何在我的通知类中显示警报对话框,并在当前活动中处理该对话框 请在这方面指导我。任何帮助都将不胜感激。这将通过将上下文发送到另一个活动来实现 public class Message { public static void alert_msg(Context context, String title, String message) { AlertDialog al

我在某些情况下发送通知,如果应用程序位于后台,则通过通知通知用户;如果用户位于前台,则通过警报通知用户。现在我想知道如何在我的通知类中显示警报对话框,并在当前活动中处理该对话框


请在这方面指导我。任何帮助都将不胜感激。

这将通过将上下文发送到另一个活动来实现

public class Message {

 public static void alert_msg(Context context, String title, String message) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Set Dialog Title
    alertDialog.setTitle(title);

    // Set Dialog Message
    alertDialog.setMessage(message);


    // Set OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    // Show Alert Message
    alertDialog.show();
 }
}
用这个打电话

  Message.alert_msg(MainActivity.this,"Title","Your Message Here"); 

您需要设置意图和广播接收器。这将允许您从通知活动中广播和显示意图,如果您的应用程序位于前台,则应用程序中配置的广播接收器可以拾取并显示对话框


->通过在参数中传递DialogInterface.OnClickListener()对象并在活动类中实现它

AlertDialogs alertdialog;

    alertdialog.SingleSelectDialog("title",otherParameters,new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          int x=alertdialog.getAmount();

                        }
                    });
->课内提醒对话框

        public void SingleSelectWithImage(String head,otherParameters, DialogInterface.OnClickListener pressok) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.MyDialogTheme);
        dialogBuilder.setTitle(head);

              amount=bla_bla;    
               //do anything
        dialogBuilder.setPositiveButton("ok", pressok);
        dialogBuilder.setNegativeButton("Cancel", null);
        dialogBuilder.show();        

}
->吸气剂法

 public int getAmount() {
        return amount;
    }

好的。谢谢。但是我在MainActivity.this上出错了。我如何才能将我的活动传递给Message.alert\u msg参数?您必须只将上下文传递给该方法…放在另一个类中。好的。我按照您所说的做了,但它给了我错误,因为ARCA在alertDialog.show()的行上捕获了我的包的badtoken异常。谢谢您的参考。没问题!如果您对整个广播接收器的事情感到有点迷茫,请告诉我们,如果您提供一些代码,也许我们可以提供帮助。一旦你理解了,它们就非常非常简单。是的,乍一看似乎很难理解。太棒了!很乐意帮忙。这是我的答案,