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
220
31
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

FAQ: Can I run a stopwatch in an Andriod notification?

Can I run a stopwatch in an Android notification?

Yes, you can run a stopwatch in an Android notification by using certain apps that offer this feature. These apps allow you to start, stop, and reset the stopwatch directly from the notification panel.

Are there any specific apps that allow running a stopwatch in an Android notification?

Yes, there are several apps available on the Google Play Store that allow you to run a stopwatch in an Android notification. Some popular options include "Floating Stopwatch," "Floating Timer," and "Floating Stopwatch & Timer."

How do I set up a stopwatch in an Android notification?

To set up a stopwatch in an Android notification, you will need to download and install a stopwatch app that supports this feature. Once installed, you can start the stopwatch from the app and then enable the notification option to display the stopwatch in the notification panel.

Can I customize the appearance of the stopwatch in the Android notification?

Some stopwatch apps allow you to customize the appearance of the stopwatch in the Android notification. You can usually change the color, size, and layout of the stopwatch to suit your preferences.

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

Running a stopwatch in an Android notification should not significantly drain your battery, as most stopwatch apps are designed to be lightweight and energy-efficient. However, it is always a good idea to monitor your battery usage when using any app continuously.

Back
Top