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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:44:20+00:00 2026-05-29T07:44:20+00:00

I am having a hard time figuring out how to display the current time

  • 0

I am having a hard time figuring out how to display the current time every fixed time interval in the status bar using service with its own thread. The following code allows me to display the current time when I click the start service button (there is also a stop service button) but it does not display the current time again. I suspect some sort of loop or timer is needed or perhaps my thread sleep mechanism is not correct. Please hint or suggest a solution. The run method is where help is needed (first). Thanks much in advance!

public class MyOwnService extends Service {
// Use a layout id for a unique identifier
private static int TIME_NOTIFICATIONS = R.layout.status_bar_notifications;

// variable which controls the notification thread
private ConditionVariable mCondition;

private NotificationManager mNM;

// Create Runnable object
private Runnable mTask = new Runnable() {
    public void run() {
        try{

            SimpleDateFormat sdfDate = new SimpleDateFormat("hh:mm:ss");
            Date now = new Date();
            String strDate = sdfDate.format(now);
            showNotification(R.drawable.icon, strDate);
            Thread.sleep(30000);

        } catch (Exception e) {
        }

            // Show happy face for 5 seconds


        //showNotification(R.drawable.icon, strDate);
        //mCondition.block(5 * 1000);

        // Done with our work... stop the service!
        //MyOwnService.this.stopSelf();
    }
};

@Override
public void onCreate() {
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    /// Start up the thread running the service. Note that we create a
    /// separate thread because the service normally runs in the process's
    /// main thread, which we don't want to block.
    Thread notifyingThread = new Thread(
            null,                   // Thread group
            mTask,                  // Runnable object
            "NotifyingService");    // Thread name
    mCondition = new ConditionVariable(false);
    notifyingThread.start();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    mNM.cancel(MOOD_NOTIFICATIONS);
    // Stop the thread from generating further notifications
    mCondition.open();
}

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

private void showNotification(int timeId, String strTime) {
    // In this sample, we'll use the same text for the ticker and the
    // expanded notification
    CharSequence text = strTime;

    // Set the icon, scrolling text and time stamp.
    // Note that in this example, we pass null for tickerText. We update the
    // icon enough that
    // it is distracting to show the ticker text every time it changes. We
    // strongly suggest
    // that you do this as well. (Think of of the "New hardware found" or
    // "Network connection
    // changed" messages that always pop up)
    Notification notification = new Notification(timeId, text, System
            .currentTimeMillis());

    // The PendingIntent to launch our activity if the user selects this
    // notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ServiceLauncher.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this,
            getText(R.string.status_bar_notification_title), text,
            contentIntent);

    // Send the notification.
    // We use a layout id because it is a unique number. We use it later to
    // cancel.
    mNM.notify(TIME_NOTIFICATIONS, notification);
}

// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new Binder() {
    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException {
        return super.onTransact(code, data, reply, flags);
    }
};
}
  • 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-29T07:44:21+00:00Added an answer on May 29, 2026 at 7:44 am

    Run() is not a loop. You need to wrap your code in a while loop.

    You also want to add a way to stop that thread gracefully so in your while loop you should check a variable that can be set from the main thread. ie

    while(!stopped){ ... }
    

    then add this to your onDestory()

    mTask.stopped = true;

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

Sidebar

Related Questions

I am having a hard time figuring out how to return the current date
Having a hard time figuring out the best way to do this... I have
I'm having a hard time figuring out how to architect the final piece of
I'm having a hard time figuring out how Firefox and Chrome determining what fields
I am having a hard time figuring out how to show an Image (or
I am having a hard time figuring out good reasons for the dependency property.
I am having a hard time figuring out how to get the property list
I am having a hard time figuring out a reasonable way to generate a
I'm Studying up on .net reflection and am having a hard time figuring out
I'm having a hard time figuring this one out. Seems like no matter what

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.