Well, I managed to make the code that works (except it should and cannot work that easily - details are provide bellow).
The code that should work is:
//create ComponentName for accesing the widget provider
ComponentName cn = new ComponentName("com.android.quicksearchbox", "com.android.quicksearchbox.SearchWidgetProvider");
//ComponentName cn = new ComponentName("com.android.music", "com.android.music.MediaAppWidgetProvider");
//get appWidgetManager instance
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
//get list of the providers - .getAppWidgetIds (cn) does seem to be unrelated to widget hosting and more related to widget development
final List<AppWidgetProviderInfo> infos = appWidgetManager.getInstalledProviders();
//get AppWidgetProviderInfo
AppWidgetProviderInfo appWidgetInfo = null;
//just in case you want to see all package and class names of installed widget providers, this code is useful
for (final AppWidgetProviderInfo info : infos) {
Log.v("AD3", info.provider.getPackageName() + " / "
+ info.provider.getClassName());
}
//iterate through all infos, trying to find the desired one
for (final AppWidgetProviderInfo info : infos) {
if (info.provider.getClassName().equals(cn.getClassName()) && info.provider.getPackageName().equals(cn.getPackageName())) {
//we found it
appWidgetInfo = info;
break;
}
}
if (appWidgetInfo == null)
return; //stop here
//allocate the hosted widget id
int appWidgetId = myWidgetHost.allocateAppWidgetId();
//bind the id and the componentname - here's the problem!!!
appWidgetManager.bindAppWidgetId(appWidgetId, cn);
//creat the host view
AppWidgetHostView hostView = myWidgetHost.createView(getBaseContext(), appWidgetId, appWidgetInfo);
//set the desired widget
hostView.setAppWidget(appWidgetId, appWidgetInfo);
//add the new host view to your activity's GUI
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ll.addView(hostView);
Well, the problem is "appWidgetManager.bindAppWidgetId(appWidgetId, cn); " - this requires the permission BIND_APPWIDGET - in the manifest.
- even if we put, it still won't work. I read it may be reserved for system applications
However, even after adding the permission and putting the apk in the system/app folder, it still does not work (it does not make it a system app).
Regarding this problem I found this answer here:
This is deliberately not available to applications. If you want to
add a widget, you need to launch the selector UI for the user to pick
the widget which will then take care of the bind. Widgets can expose
a lot of private data of all types, so it is not safe to allow an
application to arbitrarily bind to them without the user implicitly
approving (through the selection UI).
Dianne ... Android framework engineer
However, I am uncertain but, maybe this means what we could achieve the desired functionality if we could sign the apk with the image developer's key? However, this is off the question's topic.
But if you happen to be able to explain to me how to efficiently host AppWidgets, please do and have your answer accepted by me (launcher applications in the market can do it, so it must be possible).