Wrote such code here. a request is sent to the server, the response is parsed and, depending on the response, a dialog box is created with some content that came in the response. The request is sent regularly, but when the window appears, the request is sent only 2 times and then the BroadcastReceiver is not executed, which means the next request is not sent and the window is no longer hidden. The fact is that I would like to change the contents of the dialog box when the information on the server has changed well, or when the information is no longer relevant so that it is hidden.

private void SelectMethod(Node dataNode) { switch (dataNode.getNodeName()) { case "track_get_alarm": List<Track> alar = xmlP.GetNumTracksFromNode(Track.STATUS.alarm, dataNode); if (alar.size() != 0){ DialogShow(getString(R.string.alarm), alar); break; } break; } private void DialogShow(String name, final List<Track> trk) { final CharSequence[] chr = new CharSequence[trk.size()]; for (int i = 0; i < chr.length; i++) { Track t = trk.get(i); chr[i] = getString(R.string.track) + t.id + getString(R.string.atzone) + t.zone; } Intent main = new Intent(this, MainActivity.class); main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); this.startActivity(main); isStopped = true; Vibrate(true); PlaySound(true); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(name); builder.setItems(chr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Vibrate(false); PlaySound(false); msgChecking = "запрос"; new XmlSending().execute(msgChecking); } }); builder.setCancelable(true); alarm = builder.create(); alarm.setCanceledOnTouchOutside(false); alarm.setCancelable(false); alarm.show(); } private class XmlSending extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { String resp = ""; if (!isDialog) { XMLSender sender = new XMLSender(SERVER_IP, PORT); try { resp = sender.Send(params[0]); } catch (Exception e) { e.printStackTrace(); } } if (params[0].equals(msgGetWarningAlarm)){ prevResponse = newResponse; newResponse = resp; } return resp; } @Override protected void onPostExecute(String s) { //Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show(); Node nl = xmlP.GetReply(s); if (!xmlP.CheckError(nl)) { SelectMethod(nl); } else { workError(nl); } } } private void RegisterAlarmBroadcast() { mReceiver = new BroadcastReceiver() { // private static final String TAG = "Alarm Example Receiver"; @Override public void onReceive(Context context, Intent intent) { if (isStopped){ UnregisterAlarmBroadcast(); } if (alarm != null && !prevResponse.equals(newResponse)) { if (alarm.isShowing()) { DialogStop(); } } allertContext = context; pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("sample"), 0); alarmManager = (AlarmManager)(context.getSystemService(Context.ALARM_SERVICE)); setAlarm(delay, pendingIntent); new XmlSending().execute(msgGetWarningAlarm); } }; registerReceiver(mReceiver, new IntentFilter("sample")); pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("sample"), 0); alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); setAlarm(delay, pendingIntent); } private void setAlarm(int delayInMillis, PendingIntent sender){ final int SDK_INT = Build.VERSION.SDK_INT; long timeInMillis = (System.currentTimeMillis() + delayInMillis) / 1000 * 1000; //> example if (SDK_INT < Build.VERSION_CODES.KITKAT) { alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } else if (SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } } private void UnregisterAlarmBroadcast() { alarmManager.cancel(pendingIntent); getBaseContext().unregisterReceiver(mReceiver); } 

    0