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

  • SEARCH
  • Home
  • 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 8373047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:34:53+00:00 2026-06-09T14:34:53+00:00

I’m trying to make an app widget that changes the TextView value frequently —

  • 0

I’m trying to make an app widget that changes the TextView value frequently — a stopwatch widget. To do this I’m using a runnable that I start in a handler to update the values, then set the textview to those values. For some reason, the textview does not update though. Everything works properly, the textview just won’t update.

Here is the code for the Handler and Runnable:

RemoteViews remoteViews;
long sStart, elapsedTime;
private final int REFRESH_RATE = 100;
private Handler sHandler = new Handler();
private Runnable startStopwatch = new Runnable() {
    @Override
    public void run() {
        final long start = sStart;
        elapsedTime = System.currentTimeMillis() - start;
        updateStopwatch(elapsedTime);
        sHandler.postDelayed(this, REFRESH_RATE);
    }
};


private void updateStopwatch(long time) {
    int seconds = (int) time / 1000;
    int minutes = seconds / 60;
    int hours = minutes / 60;
    long millis = time % 1000;
    seconds = seconds % 60;

    remoteViews.setTextViewText(R.id.tvStopwatchWidget, String.format("%d : %02d : %02d", hours, minutes, seconds));
    remoteViews.setTextViewText(R.id.tvStopwatchMillisWidget, String.format(". %03d", millis));

}

I set everything up in the onUpdate method, it all works properly. The onReceive method, where I should update the textview on a button click, is this:

    @Override
public void onReceive(Context context, Intent intent) {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.stopwatch_widget_layout);
    if (intent.getAction().equals(ACTION_STOPWATCH_WIDGET_START)) {
        if (sStart == 0L) {
            sStart = System.currentTimeMillis();
        } else {
            sStart = System.currentTimeMillis() - elapsedTime;
        }
        sHandler.removeCallbacks(startStopwatch);
        sHandler.postDelayed(startStopwatch, REFRESH_RATE);
    }
    ComponentName widget = new ComponentName(context, StopwatchWidgetProvider.class);
    AppWidgetManager.getInstance(context).updateAppWidget(widget, remoteViews);
    super.onReceive(context, intent);
}

This is the same code I use to update it for the actual app, which works, only this is modified for the app widget. I know that the time is properly updating — if I display it in the log it updates the time correctly. I also know that the textview will properly change, if I change it directly in the onReceive method. Everything seems to be working, but for some reason it won’t update the times when it tries to in the Runnable, so I’m really not sure how to fix this.

I’ve also tried it using a service, but that didn’t work any better. The code for it is:

public class SWStartService extends Service {

RemoteViews remoteViews;
long sStart = 0;
long elapsedTime;
private final int REFRESH_RATE = 100;
private Handler sHandler = new Handler();
private Runnable startStopwatch = new Runnable() {
    @Override
    public void run() {
        final long start = sStart;
        elapsedTime = System.currentTimeMillis() - start;
        updateStopwatch(elapsedTime);
        sHandler.postDelayed(this, REFRESH_RATE);
    }
};
String sMain, sMillis;

private void updateStopwatch(long time) {
    int seconds = (int) time / 1000;
    int minutes = seconds / 60;
    int hours = minutes / 60;
    long millis = time % 1000;
    seconds = seconds % 60;

    remoteViews.setTextViewText(R.id.tvStopwatchWidget, String.format("%d : %02d : %02d", hours, minutes, seconds));
    remoteViews.setTextViewText(R.id.tvStopwatchMillisWidget, String.format(". %03d", millis));
    Intent i1 = new Intent(getApplicationContext(), SWStartService.class);
    sMain = String.format("%d : %02d : %02d", hours, minutes, seconds);
    sMillis = String.format(". %03d", millis);
    //  Log.d("TEST TIME", String.format("%d : %02d : %02d . %03d", hours, minutes, seconds, millis));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.stopwatch_widget_layout);
    if (sStart == 0L) {
        sStart = System.currentTimeMillis();
    } else {
        sStart = System.currentTimeMillis() - elapsedTime;
    }
    sHandler.removeCallbacks(startStopwatch);
    sHandler.postDelayed(startStopwatch, 100);
    intent.putExtra("sMainWidget", sMain);
    intent.putExtra("sMillisWidget", sMillis);

    sendBroadcast(intent);
    return START_REDELIVER_INTENT;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

That service is called in the onReceive method like this:

Intent intent1 = new Intent(context, SWStartService.class);
context.startService(intent1);

I’ve tried a few other techniques to start the service, I still can’t get the TextView to update though.

I know it’s pretty sloppy, I tried several different techniques to get it working, but none of it worked and I didn’t clean up the old techniques. It didn’t work with changing the remoteViews directly, or with changing them in the appwidget class by getting the extras from the intent. Everything just returns blank/null — the intent extras, or the remoteViews, in or out of the service for some reason. And I realize this might not be very good for the battery or very efficient, but it’s bothering me that I can’t even update the TextView at all.

Any suggestions?

Edit: Someone might want the solution, so here:

//Global Variables
ComponentName widget;
AppWidgetManager awManager;

//Set them up in onReceive method
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.stopwatch_widget_layout);
    widget = new ComponentName(context, StopwatchWidgetProvider.class);
    awManager = AppWidgetManager.getInstance(context);
    if (intent.getAction().equals(ACTION_STOPWATCH_WIDGET_START)) {
        //showStopButton();
        if (sStart == 0) {
            sStart = System.currentTimeMillis();
        } else {
            sStart = System.currentTimeMillis() - elapsedTime;
        }
        sHandler.removeCallbacks(startStopwatch);
        sHandler.postDelayed(startStopwatch, REFRESH_RATE);
    }
    awManager.updateAppWidget(widget, remoteViews);
    super.onReceive(context, intent);

//update appwidget in runnable
        final long start = sStart;
        elapsedTime = System.currentTimeMillis() - start;
        updateStopwatch(elapsedTime);
        sHandler.postDelayed(this, REFRESH_RATE);
        awManager.updateAppWidget(widget, remoteViews);
  • 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-09T14:34:54+00:00Added an answer on June 9, 2026 at 2:34 pm

    You only call AppWidgetManager.updateAppWidget(widget, remoteViews) once. You need to call it every time you update the widget.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload
I know there's a lot of other questions out there that deal with this
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
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.