Richard
Richard

Reputation: 14635

Update widget onClick, start service does not seem to work

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:

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 > 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;
    }
}

And for widget provider:

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);
    }
}

In manifest:

<receiver
                android:label="@string/next_trip_text"
                android:name=".widget.Widget" >
                <intent-filter >
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>

                <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/widget_info" />
            </receiver>

And xml:

<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" >
</appwidget-provider>

The service doesn't seem to be called at all. Any ideas? :)

Thanks!

Upvotes: 3

Views: 4142

Answers (1)

Pratik Bhat
Pratik Bhat

Reputation: 7614

add this to manifest:

  <meta-data
 android:name="android.appwidget.provider"
 android:resource="@xml/-- ur appwidgetprovider location here--">
</meta-data>

also try adding:

 <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED"/> 
</intent-filter>

see if it works

Upvotes: 3

Related Questions