【发布时间】:2014-10-12 23:35:39
【问题描述】:
我有一个WidgetConfigure 类,它在将小部件放在主屏幕上后被调用。但是如何在完成配置活动后开始更新我的小部件?我想在 WidgetProvider 类中调用 onUpdate 方法,因为我的 Http 请求在那里完成。
配置类:
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();
}
}
});
}
}
我不想使用 android 文档中的示例代码,因为这里没有来自 Http 请求的数据:
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
那么有什么方法可以在我的 WidgetProvider 类中执行onUpdate() 吗?
【问题讨论】: