Reputation: 2579
I did a sample from App Widget Article, Where I used Service for update the App widget.
Now I want to programatically update the App Widget from custom interval (run time interval).
How can I do this?
Upvotes: 1
Views: 841
Reputation: 20936
Here is an example how to set create an update event for AppWidget. You can customize it for your purposes:
Intent intent = new Intent();
intent.setAction(ExampleAppWidgetProvider.MY_INTENT_ACTION);
Uri data = Uri.withAppendedPath (Uri.parse("wordwidget://wordwidget/widgetId/#"),
String.valueOf(mAppWidgetId));
intent.setData(data);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
ExampleAppWidgetConfigure.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 10000, 10*1000, pendingIntent);
You should also make an additional intent-filter for your BR that will receive the intent with action name: ExampleAppWidgetProvider.MY_INTENT_ACTION
UPDATE To your Broadcast Receiver you should add new intent-filter like this:
<intent-filter>
<action android:name="org.android.testwidget.MY_APPWIDGET_UPDATE" />
<data android:scheme="wordwidget" />
</intent-filter>
Then you create an Intent that will be received by the defined intent-filter. In my case, public static final String MY_INTENT_ACTION = "org.android.testwidget.MY_APPWIDGET_UPDATE";
Data part of the Intent is needed to create a separate intent for each AppWidget instance (because I want to update each instance in different time). After that you create Pending intent for BR and create alarmManager with this PI. In the method setRepeating
you define the start time and the interval for your AppWidget instance.
Upvotes: 3