Can I run a stopwatch in an Andriod notification?

  • Thread starter Darkmisc
  • Start date
In summary, the author is trying to create a stopwatch app that will show on the lock screen. The app works fine, but the lock screen stopwatch only updates every second or so, whereas in the app, the stopwatch ticks over every hundredth of a second. The lock screen only updates for about 20s before it freezes. When the app stops working, there is no logcat message. The author has used the same code for the lock screen timer as the in app timer, but the in app timer starts from 00:00:00, whereas the lock screen timer starts from 4847:00:00. Does anyone know what the author has done wrong?
  • #1
Darkmisc
204
27
TL;DR Summary
I've tried to run a stopwatch in a notification by constantly updating the text, but it only seems to tick over every second or so. The stopwatch is supposed to tick over every hundredth of a second. It will stop updating after about 20s and program will end.
Hi everyone

I'm making a stopwatch app and I'd like the timer to continue showing on the lock screen. I've used a notification for this, but it's not working properly. The lock screen stopwatch only updates every second or so, whereas in the app, the stopwatch ticks over every hundredth of a second. The lock screen only updates for about 20s before it freezes. The app stops working at this point. There is no logcat message when this happens.

Also, the lock screen timer shows a time of 4847:38:68 when it should be 00:38:68. I've used the same code for the lock screen timer as the in app timer. I don't know why the in app timer starts from 00:00:00, whereas the lock screen timer starts from 4847:00:00.

Does anyone know what I've done wrong? Should I be using something other than a notification?

Thanks

EDIT: I've deleted Thread.sleep(2000) part of the code and the lock screen timer still updates only every second or so.
foreground service:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;public class MyForegroundService extends Service {

    long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;
    Handler handler;
    String time;
    int Seconds, Minutes, MilliSeconds ;
    final String CHANNELID = "Foreground Service ID";
    NotificationChannel channel = new NotificationChannel(
            CHANNELID,
            CHANNELID,
            NotificationManager.IMPORTANCE_LOW
    );

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            Log.e("Service", "Service is running...");
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        ).start();

        handler = new Handler() ;

        handler.postDelayed(runnable, 0);

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

    public Runnable runnable = new Runnable() {

        public void run() {

            MillisecondTime = SystemClock.uptimeMillis() - StartTime;
            UpdateTime = TimeBuff + MillisecondTime;
            Seconds = (int) (UpdateTime / 1000);
            Minutes = Seconds / 60;
            Seconds = Seconds % 60;
            MilliSeconds = (int) (UpdateTime % 1000);
            time = Minutes + ":"
                    + String.format("%02d", Seconds) + ":"
                    + String.format("%02d", MilliSeconds/10);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MyForegroundService.this, CHANNELID);
            mBuilder.setContentTitle("asfad Point");
            mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
            mBuilder.setContentText(time);
          
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
            handler.postDelayed(this, 0);
        }

    };
}
 
Technology news on Phys.org
  • #2
Perhaps Android has some built in contraint not allow a notification update more than once a second?
 
  • #3
The only way to update the time in the notification is to re-create and re-post the notification. And the rate limit is 60 Hz.

https://stackoverflow.com/a/57120044/8387076
 
  • Like
Likes Greg Bernhardt

1. Can I run a stopwatch in an Android notification?

Yes, it is possible to run a stopwatch in an Android notification. There are various stopwatch apps available on the Google Play Store that allow you to run a stopwatch in the notification bar.

2. How do I run a stopwatch in an Android notification?

To run a stopwatch in an Android notification, you will need to download a stopwatch app from the Google Play Store. Once installed, open the app and enable the notification feature in the settings. This will allow you to run the stopwatch in the notification bar.

3. Can I customize the stopwatch in an Android notification?

Yes, most stopwatch apps on the Google Play Store allow you to customize the stopwatch in the notification bar. You can change the color, font, and size of the stopwatch display as well as add or remove additional features.

4. Will running a stopwatch in an Android notification drain my battery?

Running a stopwatch in an Android notification should not significantly drain your battery. However, if the app is constantly running in the background, it may consume some battery life. It is recommended to close the app when not in use to conserve battery.

5. Can I use a stopwatch in an Android notification while using other apps?

Yes, you can use a stopwatch in an Android notification while using other apps. The notification bar is always accessible, so you can easily switch between the stopwatch and other apps without interrupting the timer.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • General Discussion
Replies
1
Views
8K
Back
Top