Есть приложение которое регулярно делает запрос на сервер с помощью AlarmManager при определенном ответе от сервера показывает AlertDialog. Когда выполняется метод onReceive если AlertDialog на экране он закрывается и выполняется очередной запрос если запрос положительный окно опять открывается. После пары перезапусков AlertDialog метод onReceive уже не выполняется.
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);
}
Подскажите как правильно реализовать этот процесс? Информация в AlertDialog может меняться потому ничего не придумал как его перезапускать но оно перезапускается только пару раз, а дальше висит без срабатываний метода onReceive.