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