Problems with android alarm

1

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

The user has a list and when he clicks he will add an alarm.

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

So far so good, the problem arises when the user changes the date to 1 day ago.

Let's suppose:

January 27 .

The user wants an alarm for 12:30 every day, if the current time is longer than these 12:30 will only give the alarm on January 28 , if the user walks back with the calendar for the day eg 10 January just start receiving notifications again on January 28.

Another problem is that if I put my calendar for 1 day to make sure and if the time is higher than the current one it immediately triggers 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, since it is no longer altered from that moment and then to solve the above errors should be changed.

And if you know a solution to this problem, I'd be grateful.

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);
    
asked by anonymous 24.06.2016 / 17:24

1 answer

3

James, you can capture these date and time changes through 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 which 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
   }
}

Secondly, we must register in% with% of its Manifest and action that it 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 Broadcast method, you can test what action you are getting through onReceive , and then correctly set the alarm.

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

More information on these links:

Android - Broadcast Receivers

ACTIONS_TIME_CHANGED or ACTION_DATE_CHANGED, can not make them work

    
26.06.2016 / 16:42