Bonjour,
Je cherche mais je n'ai rien trouvé sur la méthode pour mettre à jour mon widget toutes les minutes (et ne pas avoir la restriction imposée par Google à 30 minutes minimum).
Si quelqu'un connait la solution...
Version imprimable
Bonjour,
Je cherche mais je n'ai rien trouvé sur la méthode pour mettre à jour mon widget toutes les minutes (et ne pas avoir la restriction imposée par Google à 30 minutes minimum).
Si quelqu'un connait la solution...
J'ai trouvé un exemple de widget qui se met à jour à un intervalle de temps paramétrable.
je ferai un tuto complet, mais en attendant, la méthode se trouve dans le onReceive :NB : voir la FAQ pour le code completCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 // Fix SDK 1.5 Bug per note here: // http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider // linking to this post: // http://groups.google.com/group/android-developers/msg/e405ca19df2170e2 @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { this.onDeleted(context, new int[] { appWidgetId }); } } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) { if (!URI_SCHEME.equals(intent.getScheme())) { // if the scheme doesn't match, that means it wasn't from the // alarm // either it's the first time in (even before the configuration // is done) or after a reboot or update final int[] appWidgetIds = intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS); for (int appWidgetId : appWidgetIds) { // get the user settings for how long to schedule the update // time for SharedPreferences config = context.getSharedPreferences(ImagesWidgetConfiguration.PREFS_NAME, 0); int updateRateSeconds = config.getInt(String.format(ImagesWidgetConfiguration.PREFS_UPDATE_RATE_FIELD_PATTERN, appWidgetId), -1); if (updateRateSeconds != -1) { Log.i(LOG_TAG, "Starting recurring alarm for id " + appWidgetId); Intent widgetUpdate = new Intent(); widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId }); // make this pending intent unique by adding a scheme to // it widgetUpdate.setData(Uri.withAppendedPath(Uri.parse(ImagesWidgetProvider.URI_SCHEME + "://widget/id/"), String.valueOf(appWidgetId))); PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT); // schedule the updating AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRateSeconds * 1000, newPending); } } } super.onReceive(context, intent); } else { super.onReceive(context, intent); } }