- #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.
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);
}
};
}