在桌面上默认添加Widget

private boolean addWeatherWidget(SQLiteDatabase db, ContentValues values,TypedArray a) {  
              
            String packageName = a.getString(R.styleable.Favorite_packageName);  
            String className = a.getString(R.styleable.Favorite_className);  
            if(packageName == null || className == null){  
                return false;  
            }  
              
            final int[] bindSources = new int[] {  
                    Favorites.ITEM_TYPE_WIDGET_WEATHER,  
            };  
  
            final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();  
            bindTargets.add(new ComponentName(packageName,  className));  
  
            boolean allocatedAppWidgets = false;  
  
            // Try binding to an analog clock widget  
            try {  
                int appWidgetId = mAppWidgetHost.allocateAppWidgetId();  
  
                values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_WEATHER);  
                values.put(Favorites.SPANX,a.getString(R.styleable.Favorite_spanX));  
                values.put(Favorites.SPANY, a.getString(R.styleable.Favorite_spanY));  
                values.put(Favorites.APPWIDGET_ID, appWidgetId);  
                db.insert(TABLE_FAVORITES, null, values);  
  
                allocatedAppWidgets = true;  
                Log.i(LOG_TAG, "addWeatherWidget -- allocatedAppWidgets = "+allocatedAppWidgets);  
            } catch (RuntimeException ex) {  
                Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);  
            }  
            // If any appWidgetIds allocated, then launch over to binder  
            if (allocatedAppWidgets) {  
                launchAppWidgetBinder(bindSources, bindTargets);  
            }  
  
            return allocatedAppWidgets;  
        }   

<weather
         launcher:packageName="com.moji.mjweather"
         launcher:className="com.moji.mjweather.CMojiWidget4x2"
         launcher:screen="3"
         launcher:x="0"
         launcher:y="1" 
         launcher:spanX="4"
         launcher:spanY="2"/> 

<widget  
launcher:packageName="..."       //widget的packageName  
launcher:className=" ..."       //实现 widget的 receiver 类的名称.  
    launcher:container="..."        //放置的位置(只能为desktop)  
        launcher:screen="..."        //放置在第几屏上  
        launcher:x="..."              //放置的x位置  
        launcher:y="..."              //放置的y位置  
        launcher:spanx="..."         //在x方向上所占格数  
        launcher:spany="..."/>       //在y方向上所占格数

你可能感兴趣的:(java)