Can I run a stopwatch in an Andriod notification?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
AI Thread Summary
The discussion revolves around issues with a stopwatch app that displays a timer on the Android lock screen via notifications. The timer updates only every second, despite the app functioning correctly and updating every hundredth of a second. The lock screen timer also incorrectly starts from 4847:00:00 instead of 00:00:00. Deleting a Thread.sleep(2000) line did not resolve the update frequency issue. It is suggested that Android may impose a limit on notification updates, potentially restricting them to once per second.
Darkmisc
Messages
222
Reaction score
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. [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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top