我正在发送某些事件的通知,如果应用程序在后台,我会通过通知通知用户,如果用户在前台,我会通过警报通知用户。现在我想知道如何从我的通知类中显示警报对话框并在Current Activity中处理该对话框。
请指导我。任何帮助将不胜感激。
我正在发送某些事件的通知,如果应用程序在后台,我会通过通知通知用户,如果用户在前台,我会通过警报通知用户。现在我想知道如何从我的通知类中显示警报对话框并在Current Activity中处理该对话框。
请指导我。任何帮助将不胜感激。
这将通过将上下文发送到另一个活动来工作
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");
您将需要设置一个 Intent 和一个广播接收器。这将允许您从通知活动中广播和意图,如果您的应用程序位于前台,则应用程序中配置的广播接收器可以将其接收并显示一个对话框。
http://developer.android.com/reference/android/content/BroadcastReceiver.html http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
-> 通过在参数中传递 DialogInterface.OnClickListener() 对象并在 Activity 类中实现它
AlertDialogs alertdialog;
alertdialog.SingleSelectDialog("title",otherParameters,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int x=alertdialog.getAmount();
}
});
-> 在类 AlertDialogs
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;
}