Reputation: 4364
I have a WidgetConfigure
class which gets called after placing the widget on the home screen. But how can I start update my widget after finishing the configure activity? I want to call the onUpdate method inside my WidgetProvider class because my Http request is done there.
Configure class:
public class ExampleAppWidgetConfigure extends Activity {
private int mAppWidgetId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_CANCELED);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
//No valid ID, so bail out.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
// config
Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
TextView vendorIDTextView = (TextView) findViewById(R.id.vendorID);
TextView apiKeyTextView = (TextView) findViewById(R.id.apiKey);
Spinner currencySpinner = (Spinner) findViewById(R.id.currencySpinner);
String vendorID = vendorIDTextView.getText().toString();
String apiKey = apiKeyTextView.getText().toString();
String currency = currencySpinner.getSelectedItem().toString();
if (vendorID.isEmpty() || apiKey.isEmpty() || currency.isEmpty()) {
Toast.makeText(getApplicationContext(), getString(R.string.emptyForm), Toast.LENGTH_LONG).show();
}
else {
// save new values in preferences
SharedPreferences settings = getApplicationContext().getSharedPreferences(ExampleAppWidgetProvider.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
Log.i("LOG", "vendorID: " + vendorID);
Log.i("LOG", "apiKey: " + apiKey);
editor.putString("vendorID", vendorID);
editor.putString("apiKey", apiKey);
editor.putString("currency", currency);
editor.commit();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
// Update the widget ?
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
});
}
}
I do not want to use the example code in the android docs, because I do not have my data from the Http request here:
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
So is there any method to execute onUpdate()
in my WidgetProvider class?
Upvotes: 0
Views: 74
Reputation: 121
You can call onUpdate method by sending broadcast to AppWidgetProvider:
void updateWidget(Context c,int[] widgets_ids,Class<?> widget_class)
{
Intent update_intent = new Intent();
update_intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
update_intent.setClass(c, widget_class);
update_intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgets_ids);
c.sendBroadcast(update_intent);
}
Invoke method in (for example) Activity, but you can do this in any other place.
updateWidget(this,new int[]{widget_id},YourAppWidgetProviderClass.class);
After this onUpdate method will be called for all widgets whose ids are passed as second parameter.
You also must have action AppWidgetManager.ACTION_APPWIDGET_UPDATE added in AndroidManifest.xml to your widget:
<receiver...
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
...
</intent-filter>
</receiver>
Upvotes: 1