我想在我的活动中添加小部件,而不必启动小部件选择器活动.我尝试过了:
直接指定一个整数ID(我总是收到充气错误)
2.得到这样的ID:
Componentname cn = new Componentname(getBaseContext(),"com.androID.quicksearchBox.SearchWidgetProvIDer"); int[] IDs = AppWidgetManager.getInstance(getApplicationContext()).getAppWidgetIDs (cn);
(数组一直为空)
这些都没有.
之后,我有这个代码,使用ID(如果我从窗口小部件选择器活动获取ID,它可以工作):
appwidgetproviderInfo withWidgetInfo = AppWidgetManager.getInstance(getApplicationContext()).getAppWidgetInfo(appWidgetID); AppWidgetHostVIEw hostVIEw = myWidgetHost.createVIEw(getBaseContext(),appWidgetID,withWidgetInfo); hostVIEw.setAppWidget(appWidgetID,withWidgetInfo); linearLayout ll = (linearLayout) findVIEwByID(R.ID.ll); ll.addVIEw(hostVIEw);
我该怎么办?谢谢!
解决方法 尝试这个:// APPWidget_HOST_ID is any number you likeappWidgetManager = AppWidgetManager.getInstance(this);appWidgetHost = new AppWidgetHost(this,APPWidget_HOST_ID);appwidgetproviderInfo newappwidgetproviderInfo = new appwidgetproviderInfo();// Get an IDint appWidgetID = appWidgetHost.allocateAppWidgetID();// Get the List of installed WidgetsList<appwidgetproviderInfo> appWidgetInfos = new ArrayList<appwidgetproviderInfo>();appWidgetInfos = appWidgetManager.getInstalledProvIDers();for(int j = 0; j < appWidgetInfos.size(); j++){ if (appWidgetInfos.get(j).provIDer.getPackagename().equals("com.androID.quicksearchBox") && appWidgetInfos.get(j).provIDer.getClassname().equals("com.androID.quicksearchBox.SearchWidgetProvIDer")) { // Get the full info of the required Widget newappwidgetproviderInfo = appWidgetInfos.get(j); break; } }// Create WidgetAppWidgetHostVIEw hostVIEw = appWidgetHost.createVIEw(this,newappwidgetproviderInfo);hostVIEw.setAppWidget(appWidgetID,newappwidgetproviderInfo);// Add it to your layoutlinearLayout ll = (linearLayout) findVIEwByID(R.ID.ll);ll.addVIEw(hostVIEw);总结
以上是内存溢出为你收集整理的Android – 将AppWidgets添加到活动全部内容,希望文章能够帮你解决Android – 将AppWidgets添加到活动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)