我正在编写一个带有配置活动的小部件,在OK单击其按钮时调用以下方法:
private void ok()
{
// ...Do Widget Configuration...
// Force an update
Context context = getApplicationContext();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
AppWidgetManager.getInstance(context).updateAppWidget(widget_id, views);
// Return the expected result
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
setResult(RESULT_OK, result);
finish();
}
Run Code Online (Sandbox Code Playgroud)
这几乎是文档中的逐字记录. widget_id保存在活动期间挖出的小部件ID onCreate().
当我在主屏幕上放置小部件的实例时,我已经验证我得到了预期的事件序列:
onReceive()得到一个ACTION_APPWIDGET_ENABLED如果它是第一个.onUpdate() 被调用(我在其中检测到未配置小部件并将某些内容绘制为默认操作).OK,ok()上面的方法被调用.The method gets all the way through to the finish() and the configuration activity goes …