Idle alarm

This works great:

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

    PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid2.this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), pendingIntent);

This does not work. I hear only the alarm.

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), 3 * 1000, pendingIntent);

I tried this too, no luck:

Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7000, pendingIntent);

What is the problem?

+3
source share
2 answers

From the PendingIntent doc for FLAG_ONE_SHOT:

this PendingIntent can only once. If set, after send () is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

So, after pendingIntent is launched for the first time, it will be canceled, and the next attempt to send it through the alarm manager will fail

Try using FLAG_UPDATE_CURRENT

+3
source

Look at your code samples in order:

AlarmManager.set - , , . AlarmManager.set, , , ( PendingIntent).

. PendingIntent , , .

, 3 , BroadcastReceiver, .

, . onReceive() . , , , , ( ) . , - , , , - .

, , android.os.Handler - , , AlarmManager, .

0

Source: https://habr.com/ru/post/1785617/


All Articles