1

I am creating a simple Android Widget that will have buttons that open different activities in the application.

public class ExampleAppWidgetProvider extends AppWidgetProvider {

  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];

      // Create an Intent to launch ExampleActivity
      Intent intentForHome = new Intent(context, WidgetExampleActivity.class);
      PendingIntent pendingIntentForHome = PendingIntent.getActivity(context, 0, intentForHome, 0);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);
      views.setOnClickPendingIntent(R.id.button, pendingIntentForHome);

      // Create an Intent to launch Second Activity
      Intent intentForSecond = new Intent(context, SecondActivity.class);
      PendingIntent pendingIntentForSecond = PendingIntent.getActivity(context, 0, intentForSecond, 0);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      views.setOnClickPendingIntent(R.id.button2, pendingIntentForSecond);


      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);
    }
  }
}

This works and opens the correct activity via each button. But I am just wondering, what the updatePeriodMillis is for. I don't need anything in the Widget to update and just want the buttons to open the application. Can I get rid of the update parts. I don't want my application to keep updating the widget.

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="10000"
    android:initialLayout="@layout/widget1">
</appwidget-provider>

Also is this the correct way of performing button clicks from a Widget. I see in some tutorials that it is done differently? Thank you.

user3013243
  • 516
  • 1
  • 4
  • 19
  • Dublicate: http://stackoverflow.com/questions/5641134/how-to-disable-widget-updateperiodmillis – mik_os Jan 09 '14 at 14:42

2 Answers2

0

updatePeriod is for refreshing the widget in some time interval. If you don't need this, set your updatePeriodMillis to 0.

Manu Zi
  • 2,300
  • 6
  • 33
  • 67
  • Thank you. Can I also remove this line of code appWidgetManager.updateAppWidget(appWidgetId, views); Also is this the correct way of performing button clicks from a Widget. I see in some tutorials that it is done differently? – user3013243 Jan 09 '14 at 14:47
0

This page describes how to create widgets with buttons. If you don't need to refresh your widget fix updatePeriodMillis to 0. Note that you cannot use updatePeriodMillis with a frequency lesser than 15 min and there is no guaranties about the refresh frequency (may have some delay). If you want to refresh with a fix frequency use Alarm or register to Intent.ACTION_TIME_TICK and manage it by yourself

For handling button event it's the right way to do that since it's the one mentioned by Android page (above).

Esus
  • 173
  • 9