Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell the age of an Android repeating alarm?
    primarykey
    data
    text
    <p>I have a <code>setRepeating()</code> alarm scheduled that I want to disable in it's <code>BroadcastReceiver</code> if it is of a certain age. I would rather disable it inside of the <code>BroadcastReceiver</code>, so if its possible that would be my first choice.</p> <p>In my <code>Activity</code>:</p> <pre><code>// Start our alarm Intent intent = new Intent(Main.this, Receiver.class); long firstTime = SystemClock.elapsedRealtime(); firstTime += 2 * 1000; // start in 2 seconds long interval = 2000; // 2 seconds for testing intent.putExtra("start", firstTime); // Tell the receiver the creation date (not working) PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, 0); // Schedule the alarm! AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, sender); </code></pre> <p>In my <code>BroadcastReceiver</code>:</p> <pre><code>@Override public void onReceive(Context context, Intent intent) { // ... alarm stuff long now = SystemClock.elapsedRealtime(); Log.i("", "Alarm Running for " + (now - intent.getLongExtra("start", now))); //getLongExtra() defaults to 'now' because there is no extra 'start' } </code></pre> <p>In LogCat I see this..</p> <pre><code>08-19 11:01:04.420: INFO/(1371): Alarm Running for 0 08-19 11:01:06.420: INFO/(1371): Alarm Running for 0 08-19 11:01:08.430: INFO/(1371): Alarm Running for 0 08-19 11:01:10.420: INFO/(1371): Alarm Running for 0 08-19 11:01:12.419: INFO/(1371): Alarm Running for 0 08-19 11:01:14.419: INFO/(1371): Alarm Running for 0 08-19 11:01:16.420: INFO/(1371): Alarm Running for 0 08-19 11:01:18.420: INFO/(1371): Alarm Running for 0 </code></pre> <p>Now this to me means that the intent does not contain the "start" extra that I gave it with <code>putExtra()</code>.</p> <p>How do I tell the age of an alarm, either by intent extras or other methods?</p> <p>Edit: I can find out how many times the Receiving class has received the broadcast by creating a <code>static int</code> and incrementing it every time the alarm code executes, but this is not a good way to find the "age".</p> <p>Update: In combination from the answer provided by Mobius, you must also pass <code>PendingIntent.FLAG_UPDATE_CURRENT</code> in <code>PendingIntent.getBroadcast()</code>, like follows:</p> <pre><code>PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload