2

So, I have developed an android widget which is supposed to work as a button. I used the basic code given here: http://developer.android.com/guide/topics/appwidgets/index.html When the button is clicked, an activity is launched. This works fine every time! However, When I log the time when the button was clicked i only get the very first time. Why this happens?

Here is the code I was asked for:

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];
        Log.d("myButton","This is only called once.Why????????")
        // 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);
    }
}

}

JustCurious
  • 1,848
  • 3
  • 30
  • 57
  • Without seeing your code this can not be answered. –  Feb 16 '12 at 12:55
  • i added some code. As i have already mentioned it is JUST the android tutorial – JustCurious Feb 16 '12 at 13:00
  • The android docs doesn't have functionality to change the color or similar things. So your code has to differ from the docs a bit and must be the underlying cause in some way *(and we need to see that part to help you)*. –  Feb 16 '12 at 13:02
  • ok. forget about adding functionality.. i removed that. I only have a log.d() which is again not called every time. My method looks exactly like this. However, the functionality eg change of button colour could be added in another widget_layout.xml file and called instead of the initial layout. – JustCurious Feb 16 '12 at 13:07

1 Answers1

0

That log statement is in the onUpdate method of the widget, and will only be called initially when the widget is created, and on the widget's update period. To get it to log on click, you can do one of two things.

A. put the log statement in the onCreate method of ExampleActivity

B. change the pending intent to update the AppWidgetProvider with a flag, then overwrite the onReceive method to do the log statment and then start ExampleActivity, if the flag is present. For example:

Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(SOME_FINAL_STRING, true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

Then in the onReceive method:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Bundle extras = intent.getExtras();

    if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) && extras != null && extras.getBoolean(SOME_FINAL_STRING) == true){
        Log.d("myButton","Should no longer be called once!");
        Intent newIntent = new Intent(context, ExampleActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newIntent);
    } else {
        super.onReceive(context, intent);
    }
}
mpallansch
  • 1,174
  • 6
  • 16