Widget onClick


Last Updated:

  1. blundell

    blundell Well-Known Member This Topic's Starter

    Joined:
    Apr 23, 2010
    Messages:
    125
    Likes Received:
    17
    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 (Text):
    1.  
    2. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    3.         final int N = appWidgetIds.length;
    4.         for(int i = N-1; i >= 0; i--){
    5.             final int appWidgetId = appWidgetIds[i];
    6.            
    7.             setAlarm(context, appWidgetId, UPDATE_RATE);
    8.            
    9.             // TODO this doesnt actually bind the onClick to the widget ARGH
    10.             // http://developer.android.com/guide/topics/appwidgets/index.html
    11.            
    12.             // Create an Intent to launch MainActivity
    13.             final Intent intent = new Intent(context, MainActivity.class);
    14.             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    15.             final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    16.             // Get the layout for the App Widget and attach an on-click listener to the button
    17.             final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
    18.             views.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent);
    19.            
    20.            
    21.             // Tell the AppWidgetManager to perform an update on the current App Widget
    22.             appWidgetManager.updateAppWidget(appWidgetId, views);
    23.         }
    24.         super.onUpdate(context, appWidgetManager, appWidgetIds);
    25.     }
    26.  
    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?
     

    Advertisement
  2. blundell

    blundell Well-Known Member This Topic's Starter

    Joined:
    Apr 23, 2010
    Messages:
    125
    Likes Received:
    17
    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.
     

Share This Page

Loading...