Problems with android alarm

Asked

Viewed 207 times

1

The idea is to send a notification to the user every 24 hours.

User has a list and when click will add an alarm.

If the current time is longer than the time of the alarm we will add 24 hours for it to repeat only in the next day and not immediately.

So far all well the problem arises when the user changes the date to 1 day ago.

Say :

Day 27 of January.

The user wants an alarm for 12:30 every day, in case the current time is longer than this 12:30 will only give the alarm in the day 28 January, if the user walks back with the calendar for the day for example January 10 only starts receiving notifications again on January 28.

Another problem is also that if I set my calendar for 1 day and if the time is longer than the current one triggers immediately the alarm.

I would like to know your opinion and if it is correct to set an alarm dependent on a click of the user, because it from that time is never changed and maybe to solve the above errors should be changed .

And if you can find a solution to this problem,.

PendingIntent alarmIntent;


    Intent intent = new Intent(HorariosActivity.this, AlarmReceiver.class);

    intent.putExtra("id", favId);

    alarmIntent = PendingIntent.getBroadcast(HorariosActivity.this, favId, intent, 0);

    Calendar calendar = Calendar.getInstance();
    long agora = calendar.getTimeInMillis();


    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 00);
    firstTime = calendar.getTimeInMillis();
    if (agora > firstTime) {
        calendar.add(Calendar.HOUR, 24);
        firstTime = calendar.getTimeInMillis();
    }
    AlarmManager am = (AlarmManager) HorariosActivity.this.getSystemService(Context.ALARM_SERVICE);


    am.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 86100000, alarmIntent);
  • Tiago, when you refer to the user changing the date, do you refer to the Android date or the alarm date? Let’s take your example, when you say walk back on the date January 10th, you mean which of the two?

  • Change android date

1 answer

3


James, you can capture these date and time changes through a Broadcast, so whenever the user changes the device time, you can set the alarm correctly according to the new date.

Some of the actions you can intercept are these:

ACTION_TIME_TICK

ACTION_TIME_CHANGED

ACTION_TIMEZONE_CHANGED

First you have to create your class Broadcast who will be responsible for receiving the action:

Myreceiver.java

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      // Configure o alarme corretamente aqui
   }
}

Second, we must register at Manifest your Broadcast and the action he will intercept.

Manifest.xml

<receiver android:name="MyReceiver">

   <intent-filter>
       <action android:name="android.intent.action.ACTION_TIME_CHANGED">
       </action>
   </intent-filter>

</receiver>

Within the method onReceive you can test what action you are receiving through the intent, and thus make the correct alarm configuration.


Reading some reports, there is a bug that when the user sets the time back, the action notification is only sent when the clock picks up the next day. You’d better run the tests to see if this really happens.

More information on these links:

Android - Broadcast Receivers

ACTION_TIME_CHANGED or ACTION_DATE_CHANGED, can’t make them work

  • Just one more question I have multiple alarms coming from mysql database for my android and wanted to know if it was possible at any time mainly offline if the user change his hours send the notifications??

Browser other questions tagged

You are not signed in. Login or sign up in order to post.