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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:58:20+00:00 2026-06-04T13:58:20+00:00

I’m interesting building a simple clock widget here. And I wonder what is the

  • 0

I’m interesting building a simple clock widget here. And I wonder what is the best practice to do it? Most of the time it works fine but some says my clock widget lags behind. Actual time is 10.00am then my widget shows perhaps 9.48am

I have this on my manifest

    <receiver
        android:name="my.package.name.MyClock"
        android:label="@string/widget_name">

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

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


    <service
        android:name="MyClock$UpdateService"
        android:label="UpdateService" >
        <intent-filter>
            <action android:name="my.package.name.UPDATE" />
        </intent-filter>             
    </service>

And this is my main java class

public class MyClock extends AppWidgetProvider {

@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
    context.stopService(new Intent(context, UpdateService.class));
}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    context.startService(new Intent(UpdateService.ACTION_UPDATE));
}    

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

public static final class UpdateService extends Service {
    static final String ACTION_UPDATE = "my.package.name.UPDATE";

    private final static IntentFilter sIntentFilter;

    private String mMinuteFormat;
    private String mHourFormat;

    private Calendar mCalendar;

    static {
        sIntentFilter = new IntentFilter();
        sIntentFilter.addAction(Intent.ACTION_TIME_TICK);
        sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        reinit();
        registerReceiver(mTimeChangedReceiver, sIntentFilter);
    }

  @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mTimeChangedReceiver);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        reinit();
        update();
    }

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

    private void update() {
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        final CharSequence minute = DateFormat.format(mMinuteFormat, mCalendar);
        final CharSequence hour = DateFormat.format(mHourFormat, mCalendar);

        RemoteViews views = null;             views = new RemoteViews(getPackageName(), R.layout.main);
        views.setTextViewText(R.id.HOUR, hour);
        views.setTextViewText(R.id.MINUTE, minute);                   

        //Refresh the widget
        ComponentName widget = new ComponentName(this, MyClock.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(widget, views);          
    }    

    private void reinit() {
        mHourFormat = "hh";
        mMinuteFormat = "mm";            
    }

    private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                action.equals(Intent.ACTION_TIMEZONE_CHANGED))
            {
                reinit();

            }

            update();

        }
    };

}     }

What am I missing? Why the widget lags behind?
Can you please help me spot the issue here?
And am I doing correct approach? Using Service not AlarmManager to have clock widget updates each minute?

Regards

  • 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-04T13:58:22+00:00Added an answer on June 4, 2026 at 1:58 pm

    Quite a lot of reason that may leads to this problem, but most probably, is the Service is killed by System. There’s no way to prevent a background service being killed by System, only making it foreground service will be safe in most of the time, but the notification icon is very annoying to user.

    I think using AlarmManager would be the best, I recently updated my clock widget using this technique too. Since AlarmManager makes broadcast, even your application is killed, it will recreate it before sending.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.