Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A few things... </p> <ol> <li><code>onEnabled</code> in an AppWidgetProvider is only called once when the first appwidget is added (that's when this AppWidgetProvider becomes "enabled"). Notice that onEnabled doesn't give you an <code>appWidgetId</code> - it's not a callback associated with a particular instance of an app widget on the home screen.</li> <li>You are calling <code>getStringExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)</code> on an Intent you've just created. I think you meant to call <code>putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)</code>. However, as mentioned above, this won't work either since you aren't given an id in <code>onEnabled()</code>.</li> </ol> <p>If you want to set an alarm that is associated to each appwidget instance on the home screen, you need to do some extra work.</p> <ol> <li>When you finish configuring an app widget, store its <code>appWidgetId</code> in SharedPreferences (you could use the key "appwidgetid_##" to store a boolean value, for instance).</li> <li>When <code>onUpdate()</code> is called and you are iterating over the <code>appWidgetIds</code> array, check each <code>appWidgetId</code> in SharedPreferences first. If the check passes, you know the user has configured that appWidget and you can create and set your alarm for it; otherwise, <code>continue</code> to the next <code>appWidgetId</code>.</li> <li>When setting the alarm, note that <code>Intent</code>s must be unique when creating <code>PendingIntent</code>s, otherwise you'll get a <code>PendingIntent</code> that reuses or overwrites the old one (depending on which flag you specify as the last argument to the <code>PendingIntent</code> call). Since extras are not considered when checking for uniqueness, see the code at the bottom for how to make it unique.</li> <li>In <code>onDelete()</code>, cancel the alarm for that appwidget. Make sure you construct the PendingIntent the exact same way. You can also remove the <code>appWidgetId</code> from SharedPreferences here.</li> </ol> <p>To make the intent unique:</p> <pre><code>Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWIdgetId); // IMPORTANT! intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); // Note the FLAG_UPDATE_CURRENT PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = ... am.setInexactRepeating(...); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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