Click event on a widget

Put your problem here if it does not fit any of the other categories.

Click event on a widget

Postby blundell » Sat Jul 03, 2010 4:48 pm

My turn to ask a question :-)

So i've written a widget that basically just opens an activty.

I've followed the Android Developer example:
http://developer.android.com/guide/topi ... index.html

And implemented this code:
Code: Select all
public class ExampleAppWidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}


But it seems that appWidgetManager.updateAppWidget(appWidgetId, views); Is not updating the widget and therefore clicking on the widget does nothing.

The funny thing is, if I then redeploy my app the previous widget will have the onClick added and any new widgets I add will not.

I have to be missing something with widget ID's or something, this ring any bells for anyone?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: Click event on a widget

Postby blundell » Sat Jul 03, 2010 11:52 pm

Read the PendingIntent.getActivity api it says
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent


But the google doc example doesn't do this, bit confusing....
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Click event on a widget

Postby blundell » Sun Jul 04, 2010 12:20 pm

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.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests