问题
I am trying develop an android application for a device (device is not a tablet or phone, its a preconfigured android board running Android 4.1.1)and one of the activity need to display a customized dashboard of several types of information as shown below

The types of information shown here is preconfigured. Instead of developing all these from scratch, I am planning to install apps that has widgets (like weather and calendar) and include widgets in my Layout.
I refered to the below links
Add widgets into my own application.- Android
http://www.anddev.org/appwidgethost_tutorial-t10329.html
I completed most of my code.
public class MainActivity extends Activity {
private AppWidgetManager mAppWidgetManager;
private AppWidgetHost mAppWidgetHost;
static final int APPWIDGET_HOST_ID = 2037;
private AppWidgetHostView widgets[] = new AppWidgetHostView[16];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
setContentView(r.id.layout_main);
****here is where I am stuck??****
int [] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
mAppWidgetHost.startListening();
}
But the place where I am stuck is, how do I specify widget manager to get the specific widget I want. Let us say I am interested in the widget "com.android.deskclock/com.android.alarmclock.AnalogAppWidgetProvider"
Please advice.
Update: I added the below code
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
ComponentName name = new ComponentName("com.android.deskclock","com.android.alarmclock.AnalogAppWidgetProvider");
int [] ids = mAppWidgetManager.getAppWidgetIds(name);
if(ids != null && ids.length>0){
System.err.println("Number of widget ids :"+ids.length);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(ids[0]);
appWidgetId = ids[0];
LinearLayout parent= (LinearLayout) findViewById(R.id.generalDashboardLayout);
parent.addView(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
}else{
System.err.println("Ids is null");
}
It always prints "Ids is null" It is not finding the widget to add to my view
Please help
回答1:
Finally I solved the issue by using the below code
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
ComponentName name = new ComponentName("com.android.deskclock","com.android.alarmclock.AnalogAppWidgetProvider");
int [] ids = mAppWidgetManager.getAppWidgetIds(name);
if(ids != null && ids.length>0){
System.err.println("Number of widget ids :"+ids.length);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(ids[0]);
appWidgetId = ids[0];
LinearLayout parent= (LinearLayout) findViewById(R.id.generalDashboardLayout);
parent.addView(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
}else{
System.err.println("Ids is null");
}
回答2:
Yes you can. You need to make use of AppWidgetHost class, just like all the Home Screen applications does.
Here's a source code home screen app that you can use to learn how to use it. http://code.google.com/p/android-launcher-plus/
来源:https://stackoverflow.com/questions/13481503/can-i-add-widgets-from-installed-apps-to-my-application-activity