Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use this code to activate periodic alarms via broadcast receiver:</p> <pre><code>public class UpdateReceiver extends BroadcastReceiver { public static final String LOG_TAG = "camerawatch.alarmactivator"; public static final String ACTION = "de.pribluda.android.camerawatch.UPDATE_ALARM"; public static void activate(Context context) { AlarmManager alarmService = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE); Log.d(LOG_TAG, "pending intent: " + pendingIntent); // if no intent there, schedule it ASAP if (pendingIntent == null) { pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // schedule new alarm in 15 minutes alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent); Log.d(LOG_TAG, "scheduled intent: " + pendingIntent); } } @Override public void onReceive(Context context, Intent intent) { Log.d(LOG_TAG, "received intent: " + intent); CameraWidgetProvider.displayCurrentState(context); } } </code></pre> <p>Note, that activation is done in static method, so I can call it from almost everywhere. I schedule alarm if and only there is no such pending intent. As for context - it is available virtually everywhere - In activity ( it is context itself ) in broadcast receiver (serice method parameter) etc. </p> <p>PS: broadcast receiver is preferable to service, because it does not run all the time. When service method returns, it is stopped and application is potentially disposed ( unles it does something else, but mostly it stays around in background). This provides better battery life</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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