Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate widget onClick, start service does not seem to work
    text
    copied!<p>I've followed a bunch of tutorials, search on google and on stack overflow and came up with this code to update my widget when i touch it:</p> <pre><code>public class WidgetService extends Service{ @Override public void onStart(Intent intent, int startId) { Log.i("WidgetService", "Called"); String fakeUpdate = null; Random random = new Random(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this .getApplicationContext()); int[] appWidgetIds = intent .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); if (appWidgetIds.length &gt; 0) { for (int widgetId : appWidgetIds) { int nextInt = random.nextInt(100); fakeUpdate = "Random: " + String.valueOf(nextInt); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget); remoteViews.setTextViewText(R.id.txt_updated, fakeUpdate); appWidgetManager.updateAppWidget(widgetId, remoteViews); } stopSelf(); } super.onStart(intent, startId); } @Override public IBinder onBind(Intent intent) { return null; } } </code></pre> <p>And for widget provider:</p> <pre><code>public class Widget extends AppWidgetProvider{ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); Intent intent = new Intent(context.getApplicationContext(), WidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); PendingIntent pendingIntent = PendingIntent.getService( context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.cnt_widget, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); context.startService(intent); } } </code></pre> <p>In manifest:</p> <pre><code>&lt;receiver android:label="@string/next_trip_text" android:name=".widget.Widget" &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_info" /&gt; &lt;/receiver&gt; </code></pre> <p>And xml:</p> <pre><code>&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="272dp" android:minHeight="72dp" android:updatePeriodMillis="0" android:initialLayout="@layout/widget" android:configure="se.webevo.basttrafik.widget.WidgetConfigActivity" &gt; &lt;/appwidget-provider&gt; </code></pre> <p>The service doesn't seem to be called at all. Any ideas? :)</p> <p>Thanks!</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