Possible to include an Android App Widget inside an existing app (AppWidgetHost), without the Select Widgets list?
We're trying to host a 3rd party widget inside our own app, and we're trying to figure out how it can be added upon ou开发者_JAVA技巧r app's installation, without having the user need to select it from the Select Widgets list. The reason being, our client needs their users to choose this specific widget (and we're trying to keep the code separate so they can update this specific widget, inside our app, on their own), and it's not a great UX for a user to be able to choose a different one by accident. Is there a way to filter the Select Widget list even? Or just have it show the one we need it to show? And since we have access to this 3rd party widget's source, would that help?
We can tell so far that the only way to add a widget to a widget host is to allocate an id, run ACTION.APPWIDGET_PICK and user can choose from there.. But we really need to find a way for either that list to be filtered somehow, or this third party widget be installed when our app (host) is installed too.
Any ideas?
Thanks!
You cannot filter the list, and you cannot install the app without user clicking it. But you can inspect the results of the user's selection and reject it if they picked the wrong Widget...
This is how you embed a widget in your app without the select widgets list. The user will just see a confirmation dialog.
public boolean createWidget(View view, String packageName, String className) {
// Get the list of installed widgets
AppWidgetProviderInfo newAppWidgetProviderInfo = null;
List<AppWidgetProviderInfo> appWidgetInfos;
appWidgetInfos = mAppWidgetManager.getInstalledProviders();
boolean widgetIsFound = false;
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);
widgetIsFound = true;
break;
}
}
if (!widgetIsFound) {
return false;
} else {
// Create Widget
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
LinearLayout widgetLayout = view.findViewById(R.id.widget_view);
widgetLayout.addView(hostView);
// And bind widget IDs to make them actually work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
if (!allowed) {
// Request permission - https://stackoverflow.com/a/44351320/1816603
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
final int REQUEST_BIND_WIDGET = 1987;
startActivityForResult(intent, REQUEST_BIND_WIDGET);
}
}
return true;
}
}
精彩评论