I already know that AlertDialog
can't be called from non-activity class. I also know that I am unable to use AlertDialog
in a static method (so I could call the method from the class).
我已经知道无法从非活动类调用AlertDialog。我也知道我无法在静态方法中使用AlertDialog(所以我可以从类中调用该方法)。
The goal I want to achieve is that when user receives push notification and clicks on it, a Receiver
class is called. In some cases, the class is starting a new activity
, but in other cases, it should show alert
saying something like(that is not really written in alert): You have that activity already running, please stop the activity to start the one from notification.
我想要实现的目标是当用户收到推送通知并点击它时,会调用Receiver类。在某些情况下,该类正在启动一个新活动,但在其他情况下,它应该显示警告说类似的内容(实际上并未写入警报):您已经运行了该活动,请停止活动以启动该活动通知。
I can't achieve it, any idea?
我无法实现它,任何想法?
CODE (Receiver class):
CODE(接收器类):
@Override
public void onPushOpen(Context context, Intent intent) {
JSONObject pushData = null;
String notificationMessage = "";
Boolean inBattle = getInBattleFromCache(context);
if (inBattle){
showWarningAlert();
}else {
try {
pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
notificationMessage = pushData.optString("alert");
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("Push", "Clicked:" + pushData);
if (notificationMessage.contains("Your battle")) {
moveToMainActivity(context);
} else {
moveToArena(notificationMessage, true, context);
}
}
}
private void showWarningAlert() {
AlertDialog alertDialog = new AlertDialog.Builder(NoIdeaWhatShouldBeHere.this).create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("You are currently in a battle");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
When I place context
variable there it throws this error:
当我在其中放置上下文变量时,它会抛出此错误:
05-11 15:27:07.665 1692-1692/com.parse.starter E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.parse.starter.Receiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
at android.app.ActivityThread.access$1500(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:563)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.parse.starter.Receiver.showWarningAlert(Receiver.java:61)
at com.parse.starter.Receiver.onPushOpen(Receiver.java:32)
at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:115)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
at android.app.ActivityThread.access$1500(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
This didn't help.
这没有用。
1 个解决方案
#1
private void showWarningAlert(Context context) { //Added argument
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Use context
alertDialog.setTitle("Warning");
alertDialog.setMessage("You are currently in a battle");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
Assuming context is correct in onPushOpen
假设onPushOpen中的上下文是正确的
showWarningAlert(context);
#1
private void showWarningAlert(Context context) { //Added argument
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Use context
alertDialog.setTitle("Warning");
alertDialog.setMessage("You are currently in a battle");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
Assuming context is correct in onPushOpen
假设onPushOpen中的上下文是正确的
showWarningAlert(context);