Aubtin Samai
Aubtin Samai

Reputation: 1361

Android Widget Button Click On Add

When I add my widget from the widget page on my phone, it plays the toast message "Button Clicked." The toast is only suppose to be displayed once you push the button, and it works, but it also is displayed when the widget is added to the screen..

I am guessing it may have to do with the super, but I am not exactly sure.

Code:

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

           for (int i=0; i<N; i++) {
               int appWidgetId = appWidgetIds[i];

               Intent intent = new Intent(context, Provider.class);
               intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
               PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

               RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
               views.setOnClickPendingIntent(R.id.onoffbutton, pendingIntent);

               appWidgetManager.updateAppWidget(appWidgetId, views);
           }
       }
           @Override 
           public void onReceive(Context context, Intent intent) {
               super.onReceive(context, intent);
               if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
                   Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
                   System.out.println("Click!");
                   }

           }

EDIT:

public static String BUTTON_CLICK = "android.appwidget.action.BUTTON_CLICK";


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

           for (int i=0; i<N; i++) {
               int appWidgetId = appWidgetIds[i];

               Intent intent = new Intent(context, Provider.class);
               intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
               Intent clickIntent = new Intent(context, Provider.class);
               clickIntent.setAction(BUTTON_CLICK);  
               PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

               RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
               views.setOnClickPendingIntent(R.id.onoffbutton, pendingIntent);

               appWidgetManager.updateAppWidget(appWidgetId, views);
           }
       }
          @Override 
           public void onReceive(Context context, Intent clickIntent) {
               super.onReceive(context, clickIntent);
               if (clickIntent.getAction().equals(BUTTON_CLICK)) {
                   Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
                   System.out.println("Click!");
                   }

           }

Upvotes: 1

Views: 477

Answers (1)

Sam
Sam

Reputation: 1662

That's because you have same action for widget update

Make a separate intent action for button(BTN_ACTION for example). In onReceive() check which i=action intent has and if intent.getAction().equals(BTN_ACTION) - handle button click.

Upvotes: 2

Related Questions