Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:19:20+00:00 2026-05-23T05:19:20+00:00

with this tutorial i have created a widget with buttons. In the code below

  • 0

with this tutorial i have created a widget with buttons. In the code below when i click on ButtonP1 a toast msg is seen. I try do the same with ButtonP2 also, but only the one toast msg is seen that is set to the ButtonP1. How can i do that another toast msg appears when user clicks on ButtonP2?

    public class HelloWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        context.startService(new Intent(context, UpdateService.class));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain);

         Intent active = new Intent(context, HelloWidget.class);
         active.setAction(ACTION_WIDGET_RECEIVER);
         active.putExtra("msg", "Message for Button P1");

         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
         remoteViews.setOnClickPendingIntent(R.id.ButtonP1, actionPendingIntent);

         Intent active2 = new Intent(context, HelloWidget.class);
         active2.setAction(ACTION_WIDGET_RECEIVER);
         active2.putExtra("msg", "Message for Button P2");

         PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, active2, 0);
         remoteViews.setOnClickPendingIntent(R.id.ButtonP2, actionPendingIntent2);

         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
   }

@Override
public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) 
    {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) 
        {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } 
    else 
    {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
        {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
                } catch (NullPointerException e) {
                Log.e("Error", "msg = null");
                }
                Toast.makeText(context, "Out: " + msg, Toast.LENGTH_SHORT).show();
        }
        super.onReceive(context, intent);
    }
}
}

I have tried this (ACTION_WIDGET_RECEIVER2 instead of ACTION_WIDGET_RECEIVER)

Intent active2 = new Intent(context, HelloWidget.class);
     active2.setAction(ACTION_WIDGET_RECEIVER2);
     active2.putExtra("msg", "Message for Button P2");

     PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, active2, 0);
     remoteViews.setOnClickPendingIntent(R.id.ButtonP2, actionPendingIntent2);

with

if (intent.getAction().equals(ACTION_WIDGET_RECEIVER2)) 
                {
                    String msg2 = "null";
                    try {
                        msg2 = intent.getStringExtra("msg2");
                        } catch (NullPointerException e) {
                        Log.e("Error", "msg = null");
                        }
                        Toast.makeText(context, "Out2: " + msg2, Toast.LENGTH_SHORT).show(); //null
                }

after the same if line. In that case the msg2 variable is null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:19:21+00:00Added an answer on May 23, 2026 at 5:19 am

    You will have to use different request codes when retrieving the PendingIntent that will perform a broadcast. If not PendingIntent.getBroadcast(..) will return an existing one in your case, with the message from your first Intent.

    private static final int REQUEST_CODE_ONE = 10;
    private static final int REQUEST_CODE_TWO = 20;
    
    ....
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ....
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_ONE, active, 0);
        ....
    
        PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, REQUEST_CODE_TWO, active2, 0);
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a dataset with the C# designer following this tutorial http://www.devx.com/dotnet/Article/28678/1954 ;
I'm trying to learn this tutorial http://www.devx.com/dotnet/Article/28678/1954 in C#, I have created the datatables
I have followed this tutorial which allowed me to create a Silverlight DataGrid that
I am going through this tutorial about PDO and have come to the point
I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/ Like
I am following this tutorial https://developer.android.com/guide/tutorials/views/hello-tabwidget.html and have completed it. Now I would actually
I am learning JPA from this tutorial . I have some confusions in understanding
I have just found this great tutorial as it is something that I need.
I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
I stumbled over this passage in the Django tutorial : Django models have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.