Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Timmmm,</p> <p>Your issue is that you are looking to the wrong object. You can't really control the <code>AppWidgetManager</code>. Its not your job, its the System's. What you CAN do is control an <code>AppWidgetHost</code>, it just requires a few semantics. Here are the basics.</p> <p><strong>EDIT: Extra Background on the Widget Binding Process</strong></p> <p>The AppWidgetManager is a singleton object that runs when the System is started. This means that every instance of every launcher uses the same AppWidgetManager. What differentiates them is their AppWidgetHost and the RemoteViews they are currently holding. The AppWidgetManager basically keeps a list of all of the active hosts and the widgets they are holding. An AppWidgetHost is not a priveleged object. That is, any activity may have a single host. Thus, an entire application may be nothing but Widgets, if they so choose. </p> <p>When you instantiate the Host, you must then add Views to it. So, basically it is a list of child Views with no mandatory parental bounds, except what your Activity gives it. First, you ask for an ID (via <code>myHost.allocateAppWidgetId()</code>). Then you use your Pick Widget Activity/Dialog. The Dialog returns the WidgetInfo. The View is retrieved when you ask the Host to create the View (via <code>createView</code>) with the WidgetInfo and the ID you asked for. It then asks the widget for its <code>RemoteView</code>.</p> <p>Finally, you bind the widget by placing the View in your Activity as a Child. This is done via the addView() method of the <code>ViewGroup</code> that holds all of your Widgets.</p> <p><strong>The Process in Action (EDITED)</strong></p> <p>First, you have to make sure you have this in your android manifest:</p> <pre><code>&lt;uses-permission android:name="android.permission.BIND_APPWIDGET" /&gt; </code></pre> <p>Next, you have to create an <code>AppWidgetHost</code> (I extend my own for my launcher). The key to the Host is to keep a reference to the <code>AppWidgetManager</code> via <code>AppWidgetManager.getInstance();</code>. </p> <pre><code>AppWidgetHost myHost = new AppWidgetHost(context, SOME_NUMERICAL_CONSTANT_AS_AN_ID); </code></pre> <p>Now, get your ID:</p> <pre><code>myHost.allocateAppWidgetId() </code></pre> <p>The next step is done by whatever method you use to get the widget info. Most times it is returned via an Intent through onActivityResult. Now, all you really have to do is use the appInfo and create the view. The WidgetId is normally provided by the pick widget activity result. </p> <pre><code>AppWidgetProviderInfo withWidgetInfo = AppWidgetManager.getInstance().getAppWidgetInfo(forWidgetId); AppWidgetHostView hostView = myWidgetHost.createView(myContext, forWidgetId, withWidgetInfo); hostView.setAppWidget(forWidgetId, withWidgetInfo); </code></pre> <p>Now you just bind the View as a child to whatever you want to bind it to.</p> <pre><code>myViewGroup.addView(hostView); </code></pre> <p>Of course, you always have to consider where and how to place it, etc. Also, you have to make sure that your <code>AppWidgetHost</code> is listening before you start adding widgets.</p> <p><code>myHost.startListening()</code></p> <p><strong>To Summarize</strong></p> <p>The Widget binding process spans many methods and steps, but all occurs through the <code>AppWidgetHost</code>. Because Widgets are coded outside of your namespace you don't have any control except for where you put them and how you size the View. Since they are ultimately code that runs in your space but outside of your control, the <code>AppWidgetManager</code> acts as a <em>neutral</em> mediator, while the <code>AppWidgetHost</code> serves as the facilitator on your app's behalf. Once this is understood, your task is simple. The steps above are all the required steps for any custom launcher (including my own).</p> <p><strong>EDIT: Final Clarification</strong></p> <p>The ICS Launcher does this as well. The <code>appWidgetManager</code> they use is just a wrapper housing the <code>AppWidgetHost</code> and the calls to the <code>AppWidgetManager</code>. I forget that very little of this is explained on the Android Development Central website.</p> <p>Hope this helps! Let me know if you need anymore details.</p> <p>FuzzicalLogic</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload