There is an application that regularly makes a request to the server using the AlarmManager, with a specific response from the server showing AlertDialog. When the onReceive method is executed, if the AlertDialog on the screen closes and another request is executed, if the request is a positive window opens again. After a couple of restarts of the AlertDialog, the onReceive method is no longer running.

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) { if (alarm.isShowing()) { DialogStop(); } } setAlarm(delay, pendingIntent); new XmlSending().execute(msgGetWarningAlarm); } }; registerReceiver(mReceiver, new IntentFilter("sample")); pendingIntent = PendingIntent.getBroadcast(MainActivity.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); } Toast.makeText(this, "New alarm!!!", Toast.LENGTH_SHORT).show(); } private void UnregisterAlarmBroadcast() { alarmManager.cancel(pendingIntent); getBaseContext().unregisterReceiver(mReceiver); } 

Tell me how to properly implement this process? The information in the AlertDialog can change because I haven't figured out how to restart it, but it is restarted only a couple of times, and then hangs without the onReceive method.

    1 answer 1

    BroadcastReceiver not for nothing that BroadcastReceiver will receive a Context as a parameter.

     public void onReceive(Context context, Intent intent) 

    I don’t see how an AlertDialog is created, but it’s very similar that the contexts are out of sync, the context that was used to create the AlertDialog no longer exists.
    Review the approach, use the context from onReceive , it can be different every time.

    • Changed the receiver, now when creating an AlertDialog use the context allertContext that is updated when mReceiver executed. But nothing has changed. AlertDialog.Builder builder = new AlertDialog.Builder(allertContext); AlertDialog dialog = builder.create(); dialog.show(); - Andrew Romanenko
    • More detailed process. It turns out that I have the alarmManager.set() method for example, but it doesn’t appear in the receiver even though I use the updated context. - Andrew Romanenko
    • It is not clear how you synchronize the receiver and network operation. Each by itself works asynchronously, stale situations can be created. - Eugene Krivenja