0

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).

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.

I can't achieve it, any idea?

CODE (Receiver class):

@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.

Community
  • 1
  • 1
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • Not familiar with onPushOpen. Error happens when context is wrong - what is being passed as context into that function? – Eric S. May 11 '15 at 19:33
  • Never mind, I will mark your answer as the right one, because I am sure thereis a problem with that context thing. And it is because of me using parse.com. I used toast instead... thanks :) – Ondrej Tokar May 11 '15 at 19:36
  • okay. Alertdialog needs the activity instance you want to display from. Usually you can get this from doing getActivity().this, but I don't know where you are calling your function from. You can use toast, but you will run into same issue. – Eric S. May 11 '15 at 19:38
  • 1
    Toast works. That function is called automatically from manifest.xml definition. – Ondrej Tokar May 11 '15 at 19:39

1 Answers1

4
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

    showWarningAlert(context);
Eric S.
  • 1,502
  • 13
  • 31
  • I swear I have tried that and it didn't work... omg. Thanks, it seems that was it. I must have tried something like `context.getClass()` or something like that... – Ondrej Tokar May 11 '15 at 19:17