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 7928345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:42:21+00:00 2026-06-03T19:42:21+00:00

I’m trying just some very simple code to get an Android widget going but

  • 0

I’m trying just some very simple code to get an Android widget going but with no luck. I’ve looked around everywhere and haven’t found a good answer.

All I want (for now) is to increment a counter when the widget is touched and display the current value.

This is my AppWidgetProvider:

public class WordWidget extends AppWidgetProvider
{
    Integer touchCounter = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        //This is run when a new widget is added or when the update period expires.
        Log.v("wotd",  "Updating " + appWidgetIds.length + " widgets");

        for(int x = 0; x < appWidgetIds.length; x++)
        {
            Integer thisWidgetId = appWidgetIds[x];

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());

            Intent widgetIntent = new Intent(context, WordWidget.class);
            widgetIntent.setAction("UPDATE_NUMBER");

            PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

            remoteViews.setOnClickPendingIntent(R.id.widgetLinearLayout, widgetPendingIntent);
            appWidgetManager.updateAppWidget(thisWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.v("wotd", "In onReceive with intent=" + intent.toString());
        if (intent.getAction().equals("UPDATE_NUMBER"))
        {
            Log.v("wotd", "In UPDATE_NUMBER");
            touchCounter++;
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());
        } else
        {
            Log.v("wotd", "In ELSE... going on to super.onReceive()");
            super.onReceive(context, intent);
        }
    }
}

This is part of my manifest:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <receiver
        android:icon="@drawable/ic_launcher"
        android:name="com.example.mywidget.WordWidget"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="UPDATE_NUMBER" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widgetinfo" />
    </receiver>

</application>

The log shows the onReceive() is called immediately after being placed on the home screen, and after being touched, however the number never increases. I don’t totally understand how widgets work, but are they killed after onUpdate()? So to do this I would have to use some kind of persistant storage?

Also, if I currently add another widget both would show the same values and increment even if I just touch one. Is there a way for each and any widget to have it’s own counter?

Thanks!

  • 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-06-03T19:42:23+00:00Added an answer on June 3, 2026 at 7:42 pm

    Actually you have answered your question. But let’s clarify some things:

    A. AppWidgets are NOT killed as long as they are on a home screen. But they don’t belong to you. They are running in the process of the home application. To be more specific their views are running in the process of the home application, this is why you are see your widget but it doesn’t do what you are expected and this is why we are using RemoteViews instead of Views in order to update them.

    B. AppWidgetProviders (in your case the WordWidget class), on the other hand, are destroyed as soon as the onReceive method finishes. All the variables in them are re-initialized every time the onReceive method gets called. This is why your number never increases. The purpose of an AppWidgetProvider is to update the widget’s RemoteViews and to inform your application that a registered broadcast has arrived.

    C. AppWidgetProvider’s onUpdate method provides you an Array with the widgets Ids that must be updated. This is the only code point you can use to get the number of your widget instances and their Ids. Because of the RemoteViews there is NO way to get some useful value from the Views of your widget (for example you can NOT read the counter value from the TextView) so you must use the provided information and DO persist your counter values per widget id. When the next onUpdate gets called you read the value from the storage, increase it, update the widget with the new value and then store the new value back.

    D. If your widget has to do many things or slow things (like networking) when its time to update itself, you should consider using a service for this. But in your case (to increase a number) your approach is just fine as long as you persist the counters in the persistent storage or somewhere else.

    Finally I ‘ve noticed that in your onReceive override you are calling the “super.onReceive” only if you don’t receive the “UPDATE_NUMBER” action. This is NOT a good practice, unless there is a GOOD reason (that’s another story) always call super.onReceive as the first or the last command in your override.

    Hope this helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.