0

Not sure if this gonna be answered by another thread, but for awhile of seeking the answer, i didn't found one.

I'm implement an app widget to display some text every 30mn, but it didn't update until I click on the widget. Here is my onUpdate methode :

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
      {
            // Get all ids
            ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
            Calendar getToday = Calendar.getInstance();
            for (int widgetId : allWidgetIds) 
            {
                  int number = (new Random().nextInt(100));

                  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                  // Set the text
                  String n = ""+number;
                  remoteViews.setTextViewText(R.id.update, n);
                  remoteViews.setTextColor(R.id.update, Color.BLUE);

                  // Register an onClickListener
                  Intent intent = new Intent(context, MyWidgetProvider.class);

                  intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                  intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

                  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                  remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
                  appWidgetManager.updateAppWidget(widgetId, remoteViews);

            }

What's the problem with my code ? Can anybody help me out. thanks in advance

Added :

<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:updatePeriodMillis="110000" 
    android:initialLayout="@layout/widget_layout" 
    android:minHeight="72dp"
    android:minWidth="300dp">
</appwidget-provider>
Edward Sullen
  • 157
  • 1
  • 5
  • 18
  • Post the content of your appwidget-provider XML file – David Wasser Nov 23 '12 at 12:22
  • I do not understand how the subject of this question "AlarmManager in onReceive() of class extended by AppWidgetProvider" relates to the content of the question or the code you've provided. – David Wasser Nov 23 '12 at 12:26
  • my question updated ! thanks – Edward Sullen Nov 23 '12 at 12:39
  • Your `onUpdate()` method should get called every 110 seconds (2 minutes). If you add logging to `onUpdate()` do you see it getting called or not? Also, have you overridden `onReceive()` in this class? – David Wasser Nov 23 '12 at 13:00
  • I didn't override onReceive(). Does 2minutes will work ? since I found some tutorial said that the minimum is 30mns ! – Edward Sullen Nov 23 '12 at 15:01
  • They suggest you don't update too often, as it can drain the battery. But there's no reason why 2 minute updated wouldn't work. Is the `onUpdate()` method getting called? – David Wasser Nov 23 '12 at 15:30
  • So, is there any better way if I just want to update when new day is come. Cos as you said, if I update lots of time per day, there will drain battery so much. ! – Edward Sullen Nov 23 '12 at 16:06
  • Sure, there are other ways of doing this. For example, you can schedule an alarm for a specific time in the future (using `AlarmManager`) and have it broadcast an Intent that will update your widget. However, this doesn't address your original problem. Is your widget getting updated every 2 minutes? Is the `onUpdate()` method getting called? – David Wasser Nov 23 '12 at 16:41
  • can you provide me link of using AlarmManager in AppWidgetProvider...With 2mns update , work not fine. but with 30mns update, it work fine. – Edward Sullen Nov 24 '12 at 00:06

1 Answers1

2

Updates will not happen more than once every 30 minutes, even if your updatePeriodMillis specifies a more frequent interval. See AppWidgetProviderInfo doc.

lseidman
  • 156
  • 3