我正在尝试为设备开发一个 android 应用程序(设备不是平板电脑或手机,它是运行 Android 4.1.1 的预配置 android 板),其中一个活动需要显示几种类型信息的自定义仪表板,如下所示
此处显示的信息类型是预先配置的。我计划安装具有小部件(如天气和日历)并在我的布局中包含小部件的应用程序,而不是从头开始开发所有这些。
我参考了以下链接
http://www.anddev.org/appwidgethost_tutorial-t10329.html
我完成了大部分代码。
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();
}
但是我卡住的地方是,我如何指定小部件管理器来获取我想要的特定小部件。假设我对小部件 “com.android.deskclock/com.android.alarmclock.AnalogAppWidgetProvider”感兴趣
请指教。
更新: 我添加了以下代码
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");
}
它总是打印“Ids is null” 它没有找到要添加到我的视图的小部件
请帮忙