Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android_alarm_manager_plus - function periodic not working #323

Closed
jeihcio opened this issue Jun 24, 2021 · 9 comments
Closed

android_alarm_manager_plus - function periodic not working #323

jeihcio opened this issue Jun 24, 2021 · 9 comments
Assignees
Labels
android_alarm_manager_plus Hacktoberfest Issues taking part in Hacktoberfest question Further information is requested Stale

Comments

@jeihcio
Copy link

jeihcio commented Jun 24, 2021

I'm using the 'plus_plugins' example itself and the periodic method is not firing, the oneShot method works perfectly. Periodic is not triggering the callback function. I believe it's some mistake of mine, could you guide me? I've already tried to find the solution elsewhere

@jeihcio jeihcio added the question Further information is requested label Jun 24, 2021
@Chinmay-KB
Copy link
Contributor

What was the interval you had passed for periodic triggering?
If you have tried just changing oneShot to periodic in the example code, the callback will fire, but not at the desired time. Since Android 5.1, the minimum interval for a periodic fire using AlarmManager is 1 minute. So even if you pass a smaller duration, it will default to 1 minute. If you want to call more frequently, you should look into Handler.

@jeihcio
Copy link
Author

jeihcio commented Jun 27, 2021

I tested several minutes for the periodic, including 15 and 30 minutes

@Chinmay-KB
Copy link
Contributor

Could you provide a code sample in which the issue can be reproduced? Also the AndroidManifest.xml. I have tried it with the example code and it worked fine on my device.

@jeihcio
Copy link
Author

jeihcio commented Jun 27, 2021

I got it here, but I had to add some code in "app/build.gradle".

implementation "com.android.support:multidex:2.0.1"

I can't explain why it worked, but it worked.

thanks!

@Chinmay-KB
Copy link
Contributor

There's an error which says number of method references in a dex file cannot exceed 64k references. That's where multidex helps. Not sure but maybe that was the issue. But as far as I know the app should not have built in the first place if this was the error 🤔

@jeihcio
Copy link
Author

jeihcio commented Jun 28, 2021

After that it started working for me, however, now I'm thinking it was just cohesiveness, because then it stopped working, then I went to look on my cell phone and turned off the battery saving restrictions (my cell phone is xiaomi) and it started working again. I believe that the real problem must be this one of restriction

@ghost
Copy link

ghost commented Aug 25, 2021

same issue. Singleshot works perfectly but the periodic just won't work

here is my code:

`

 void printHello() {
   final DateTime now = DateTime.now();
   final int isolateId = Isolate.current.hashCode;
   print("[$now] Hello, world! isolate=$isolateId function='$printHello'");
 }

class ReminderUtil {
  void onSave(
    int? _day,
    DateTime? _workStartTime,
    DateTime? _workEndTime,
    Duration? _frequency,
    Duration? _breakPeriod,
  ) {
    String? _dayName = TimeUtil().returnDayName(_day!);
    DateTime? workStartTime = TimeUtil().getNextDate(_workStartTime, _day);
    DateTime? workEndTime = TimeUtil().getNextDate(_workEndTime, _day);
    List<DateTime> listOfFrequency = TimeUtil().returnListAlarmTime(
      start: workStartTime,
      end: workEndTime,
      frequency: _frequency!,
      breakPeriod: _breakPeriod!,
    );
    List<SingleReminderModel> singleReminders = [];
    for (int i = 0; i < listOfFrequency.length; i++) {
      var random = new Random();
      var newRandom = random.nextInt(9000) + 10000;
      while (true) {
        if (Store.instance.getIdData().contains(newRandom)) {
          newRandom = random.nextInt(9000) + 1000;
        } else {
          break;
        }
      }
      SingleReminderModel singleReminderModel = SingleReminderModel(
        singleReminderId: newRandom,
        alarmStartAt: listOfFrequency[i],
      );
      singleReminders.add(singleReminderModel);
      IdModel idModel = IdModel(
        id: newRandom,
      );
      Store.instance.setSingleId(idModel);
    }

    // create another random number
    var random = new Random();
    var newRandom = random.nextInt(9000) + 1000;
    while (true) {
      if (Store.instance.getIdData().contains(newRandom)) {
        newRandom = random.nextInt(9000) + 1000;
      } else {
        break;
      }
    }
    ReminderModel? reminder = ReminderModel(
      id: newRandom,
      dayName: _dayName,
      workStartTime: _workStartTime,
      workEndTime: _workEndTime,
      singleReminderModel: singleReminders,
      frequency: _frequency,
      breakPeriod: _breakPeriod,
    );
    scheduleAlarm(singleReminders);
    addReminderToDB(reminder);
    // save to DB
    IdModel idModel = IdModel(
      id: newRandom,
    );
    Store.instance.setSingleId(idModel);
  }

  void addReminderToDB(ReminderModel object) {
    Store.instance.setSingleReminder(object);
    EventStream.putEvent(Event(EventType.REMINDER_ADDED));
  }

  void scheduleAlarm(List<SingleReminderModel> singleReminders) async {
    for (int i = 0; i < singleReminders.length; i++) {
      print(singleReminders[i].alarmStartAt);
      await AndroidAlarmManager.periodic(
        new Duration(days: 7),
        singleReminders[i].singleReminderId,
        printHello,
        startAt: singleReminders[i].alarmStartAt,
      );
    }
  }
}

`

You can focus only on the schedulealarm method the other codes are for simultaneouly reading and writing to DB.

N.B: I'm using the ID that I'm saving to DB as alarm id

@chazgps
Copy link

chazgps commented Oct 25, 2021

My callback is called one time, then, nevermore.
I've tried many different approachs and nothing did worked!

I cross Internet for someone having the same problem and maybe a solution, unfailthful I don't found anyone.

EDIT: My problem was solved using the allowWhileIdle parameter!
#487 (comment)

@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android_alarm_manager_plus Hacktoberfest Issues taking part in Hacktoberfest question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

5 participants