• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Widget onClick

blundell

Well-Known Member
Hi Guys,

I've written a widget and attached an onClick to the whole widget following the android developer site:
App Widgets | Android Developers

Code:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		for(int i = N-1; i >= 0; i--){
			final int appWidgetId = appWidgetIds[i];
			
			setAlarm(context, appWidgetId, UPDATE_RATE);
			
			// TODO this doesnt actually bind the onClick to the widget ARGH
			// http://developer.android.com/guide/topics/appwidgets/index.html
			
			// Create an Intent to launch MainActivity
            final Intent intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            // Get the layout for the App Widget and attach an on-click listener to the button
            final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent);
            
            
            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

But the problem is it doesn't actully bind the onClick event to the widget
AND
the strange thing is if I redeploy/reinstall the app, the old widgets have the onClick but any new ones I add do not.

This ring any bells for anyone?
 
Well I gave up :-)

Moved my setOnClickPendingIntent() from the widget update() to the widget's service onStart(). This is a less optimal solution as means it will be called more often, but seems like the test devices can deal with it and I've seen it in other applications.
 
Back
Top Bottom