Widgets can have a special activity called when they are added to the home screen. That activity can return a SUCCESS intent with the widget ID receive in the "onCreate" method of the activity or RESULT_CANCELED if you want to prevent the creation of the widget itself.
You just need to add the configure activity class in a android:configure
attribute at the appwidget-provider
XML and add an intent filter with android.appwidget.action.APPWIDGET_CONFIGURE
to your activity declaration at the AndroidManifest. It's detailed in http://developer.android.com/guide/topics/appwidgets/index.html#Configuring
The intent received has the widget ID:
public void onCreate(Bundle savedInstanceState) {
...
int widgetID = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
...
}
and then you can either finish your activity with setResult(RESULT_CANCELED);
or confirm the widget creation with:
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
You can do all your widget settings from that activity.
On the other hand, if you want to perform other setup that only needs to occur once for all your widgets instances, you can make then in the onEnabled
method of your AppWidgetProvider class as described in http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider