问题描述
我正在开发启动器.它使用户可以从列表中添加小部件,效果很好.当用户在屏幕上添加小部件的信息(PackageName,ClassName及其在屏幕上的坐标)时,我将其存储在数据库中.
I am working on a launcher. It let's users add widgets from the list, it works fine.I store information about the widget (PackageName, ClassName and it's co-ordinates on the screen) in my database when the user adds one to the screen.
重新启动应用程序(或设备本身)后,我通过代码将这些(用户选择的)小部件添加回去:
When the application is restarted (or the device itself) I add those (user selected) widgets back through code:
// APPWIDGET_HOST_ID is any number you like
appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo();
// Get an id
int appWidgetId = appWidgetHost.allocateAppWidgetId();
// Get the list of installed widgets
List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>();
appWidgetInfos = appWidgetManager.getInstalledProviders();
for(int j = 0; j < appWidgetInfos.size(); j++)
{
if (appWidgetInfos.get(j).provider.getPackageName().equals(PackageName) && appWidgetInfos.get(j).provider.getClassName().equals(ClassName))
{
// Get the full info of the required widget
newAppWidgetProviderInfo = appWidgetInfos.get(j);
break;
}
}
// Create Widget
AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
RelativeLayout widgetLayout = (RelativeLayout) findViewById(R.id.widgetLayout);
widgetLayout.addView(hostView);
该小部件已成功添加到布局,但似乎未启用/更新.尽管我输入了以下内容,但它不响应点击(例如,模拟时钟)并且不显示任何数据(例如,音乐播放器小部件):
The widget is successfully added to the layout but it seems like it's not enabled/updated. It doesn't respond to clicks (for instance, Analog Clock) and doesn't show any data (for instance, Music player widget), although I have put this:
@Override
protected void onStart() {
super.onStart();
appWidgetHost.startListening();
}
@Override
protected void onStop() {
super.onStop();
appWidgetHost.stopListening();
}
我坚持了很长时间.搜索了很多,但似乎我是这个问题上世界上唯一的一个人.
I am stuck on it since quite a long time. Searched a lot but it seems I'm the only one in this world with this problem.
推荐答案
我们可以调用一个对话框,要求用户允许我们的应用程序使用以下代码绑定小部件ID:
We can invoke a dialog asking user to permit our app to be able to bind widget IDs with the following code:
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, sSavedWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mWidgetInfo.provider);
startActivityForResult(intent, REQUEST_BIND_WIDGET);
并绑定窗口小部件ID,以使其实际可用于:
And bind widget IDs to make them actually work with:
Bundle opts = new Bundle();
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
widgetManager.bindAppWidgetIdIfAllowed(widgetId, mWidgetInfo, opts);
有关更多信息,请参见:
For more information refer:
- AppWidgetManager.ACTION_APPWIDGET_BIND
- AppWidgetManager.bindAppWidgetIdIfAllowed()
我希望有人能在5年前回答同样的问题.
I wish someone would have answered the same 5 years ago.
这篇关于通过代码重新添加时,小部件不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!