Can I run a stopwatch in an Andriod notification?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on issues encountered while developing a stopwatch app for Android that displays a timer in a notification on the lock screen. The timer updates inconsistently, only every second, and displays an incorrect starting time of 4847:38:68 instead of 00:38:68. The developer has attempted to resolve the issue by removing a Thread.sleep(2000) call, but the problem persists. It is suggested that Android may impose a limitation on notification updates, restricting them to a maximum of 60 updates per second.

PREREQUISITES
  • Understanding of Android Service lifecycle
  • Familiarity with NotificationCompat for Android notifications
  • Knowledge of Handler and Runnable classes in Android
  • Experience with SystemClock for time management in Android
NEXT STEPS
  • Investigate Android's notification update limitations and best practices
  • Learn about using Foreground Services for continuous background tasks
  • Explore alternatives to notifications for real-time updates, such as using a custom lock screen widget
  • Review Android's documentation on handling time-sensitive tasks in services
USEFUL FOR

Android developers, mobile app developers, and anyone interested in implementing real-time features in Android applications.

Darkmisc
Messages
222
Reaction score
31
TL;DR
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. [CODE lang="java" title="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);
}

};
}

[/CODE]
 
Technology news on Phys.org
Perhaps Android has some built in contraint not allow a notification update more than once a second?
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
3K
Replies
3
Views
3K
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K