-1

I have a Main activity that waits for random messages from a web service and processes them. After the process completes it shows the user a message. The user isn't always in the main activity, so they do not get the message even though the process is run until they return to the main activity. The question is can the user be shown the message while they are not in the main activity? This the code I am using in the main activity:

public void showAuthRequested(String val) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("auth requested");
    builder.setMessage("requesting authorization, do you accept?");


    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            authorized = false;
            SharedPreferences pref1 = getApplicationContext().getSharedPreferences("Mysets", 0); // 0 - for private mode
            SharedPreferences.Editor editor = pref1.edit();
            editor.putBoolean("authorized",false);
            editor.commit();
            webRequests("https://xxx.xxx.xxx.xxx/xxx/xxx/accessResult.php", "true");
        }
    });
    builder.setNegativeButton("Cancel", null);

    // create and show the alert dialog
    final AlertDialog dialog = builder.create();

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            dialog.show();
        }
    });

I tried adding a Broadcast receiver as per Prashant.J's comment

public class AuthReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");



AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Transfer mission requested");
builder.setMessage("EUD is requesting control, do you accept?");
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("ok",null);
final AlertDialog dialog = builder.create();
dialog.show();

}

But I got this error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.goldlink.nglsv3, PID: 14742 java.lang.RuntimeException: Unable to start receiver com.goldlink.nglsv3.AuthReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.app.ActivityThread.handleReceiver(ActivityThread.java:3614) at android.app.ActivityThread.access$1300(ActivityThread.java:238) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1798) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7073) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:1056) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:381) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.app.Dialog.show(Dialog.java:470) at com.goldlink.nglsv3.AuthReceiver.onReceive(AuthReceiver.java:27) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3605) at android.app.ActivityThread.access$1300(ActivityThread.java:238) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1798) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7073) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) I/Process: Sending signal. PID: 14742 SIG: 9 Disconnected from the target VM, address: 'localhost:8617', transport: 'sock

user295996
  • 11
  • 2
  • Have you considered using a `Notification` instead? It would certainly be more user-friendly than a `Dialog` that might pop up out of nowhere while they're busy elsewhere. – Mike M. Apr 11 '20 at 20:42
  • Using a custom broadcast will be a good fit here :) You need to send the broadcast from MAinActivity after getting your response from web service and write your AlertDialog in onReceive method of Broadcast receiver class. That's it. – Prashant.J Apr 11 '20 at 20:53
  • You can't show a `Dialog` from a manifest-registered Receiver. A `Dialog` needs an `Activity` as its `Context`. – Mike M. Apr 12 '20 at 15:04
  • Mike, the problem with the notification is the user has only 15 seconds to respond to the comment or something they might not want to happen will occur. So its imperative that they get a pop up whether they want it or not. That's in my requirements – user295996 Apr 12 '20 at 15:48
  • If it's absolutely super-imperative, then why don't you just start another `Activity`? You can even make it look like a dialog by using a `Dialog` theme, and you could call it from almost anywhere in your app (though you might have to start a new task). I would also mention that shouldn't rely on any background work being done in an `Activity` that is not currently in the foreground. You should be doing that in a `Service`, preferably a foreground `Service`, for which you're going to need a `Notification` anyway. – Mike M. Apr 12 '20 at 16:23

1 Answers1

-1

As per my comment I am writing the steps below.

  1. Write a BroadcastReceiver class with onReceive() method.
  2. Declare BroadcastReceiver inside manifest file with correct intent-filter
  3. In your MainActivity-> after you get the response from web service send this broadcast. you can putExtra any message if you want.
  4. In OnReceive() method of BroadcastReceive check for this intent action using intent.getAction()
  5. From there open activity which will show a dialog.

for more information by code. see the below link. show an alert dialog in broadcast receiver after a system reboot

Prashant.J
  • 739
  • 7
  • 12
  • 1
    Are you saying I can use the Alert Dialog in the onReceive() method? When I tried I got an exception on the show. – user295996 Apr 11 '20 at 23:35
  • Whats the exception? have you checked if you are on Ui thread? send the broadcast once you come on main thread from background :) also check if you are using correct context. Paste your error here then I can help you more. – Prashant.J Apr 12 '20 at 07:04
  • From where did you send the broadcast? You need to send the custom broadcast and check for the same inside onReceive(). you cannot directly show alert inside onReceive method. Activities can show alert only. in broadcast receiver start an activity to show the alert. – Prashant.J Apr 12 '20 at 19:26
  • I have edited my answer. Please have a look and follow the code inside the link. This will help you :) – Prashant.J Apr 12 '20 at 19:33
  • Those examples are all redundant. There's no reason to start an `Activity` just to show a `Dialog` from it. Simply set a `Theme.*.Dialog` on the `Activity`, and have it act as the `Dialog`. Just FYI. – Mike M. Apr 12 '20 at 19:42