Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Solved - I can confirm that an Activity is NOT needed if you want create a Button to update an Android AppWidget.</p> <p>I have been able to implement my AppWidgetProvider class such that it registers an android.appwidget.action.APPWIDGET_UPDATE intent-filter with the Broadcast receiver in the AndroidManifest.xml, which then fires the onUpdate event in the AppWidgetProvider class (which in turn then runs the UpdateService).</p> <pre><code>&lt;!-- Broadcast Receiver that will process AppWidget updates --&gt; &lt;receiver android:name=".MyWidget" android:label="@string/widget_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /&gt; &lt;/receiver&gt; </code></pre> <p>The UpdateService in my AppWidgetProvider class then uses onHandleIntent to run a private buildUpdate method - which registers the onClick event with a call to setOnClickPendingIntent as follows:</p> <pre><code>// set intent and register onclick Intent i = new Intent(this, MyWidget.class); PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0); updateViews.setOnClickPendingIntent(R.id.update_button,pi); </code></pre> <p>Here is a link to some source code of a working example, which shows how an update button can be used to update a Twitter widget:</p> <p><a href="https://github.com/commonsguy/cw-advandroid/tree/b01438e7f0fed8f795ddec4be43066905f03d0cc/AppWidget/TwitterWidget" rel="noreferrer">https://github.com/commonsguy/cw-advandroid/tree/b01438e7f0fed8f795ddec4be43066905f03d0cc/AppWidget/TwitterWidget</a></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