我已经知道AlertDialog
无法从非活动类调用。我也知道我无法在静态方法中使用AlertDialog
(所以我可以从类中调用该方法)。
我想要实现的目标是,当用户收到推送通知并点击它时,会调用Receiver
类。在某些情况下,该课程正在开始新的activity
,但在其他情况下,它应该显示alert
类似的内容(不是真的写在警报中):You have that activity already running, please stop the activity to start the one from notification.
我无法实现它,任何想法?
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();
}
当我在其中放置context
变量时,会抛出此错误:
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没有帮助。
答案 0 :(得分:0)
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();
}
假设onPushOpen中的上下文正确
showWarningAlert(context);