StopWatch Timer on Service Android Studio Kotlin Tutorial

  Рет қаралды 26,989

Code With Cal

Code With Cal

Күн бұрын

How to create a stop watch timer app that runs in the background using Android Studio and Kotlin. In this example project build a stopwatch app that counts up from zero. The timer is running on a service meaning it runs on a background thread and not the main UI Thread. The advantage of running a timer on a service is that if the screen rotates the timer continues to count. If the app it put to the background the timer will continue to count up as expected.
The stopwatch timer will be able to start, pause and reset.
Timer on Service Source Code:
github.com/codeWithCal/TimerO...
Java Android Studio Timer Tutorial:
• Count Up Timer App And...
Android Services Documentation:
developer.android.com/guide/c...
⏱️ TIMESTAMPS ⏱️
00:00 - Intro
00:30 - Layout XML
02:23 - Build Gradle
02:36 - Timer Service
04:22 - Main Kotlin
07:50 - Android Manifest
#TimerAppTutorial #LearnAndroidStudio #KotlinProgramming

Пікірлер: 91
@ousaidanass1383
@ousaidanass1383 Ай бұрын
Thank you so much
@hlkihglkhglkhg
@hlkihglkhglkhg 2 жыл бұрын
You sir are a legend, thank you!
@CodeWithCal
@CodeWithCal 2 жыл бұрын
😁
@happyroden8826
@happyroden8826 2 жыл бұрын
thank you, good video explanation, i've subscribed
@jerichosantonia6412
@jerichosantonia6412 Жыл бұрын
thanks man!
@armangukasian
@armangukasian Жыл бұрын
where did we get the startService and stopService method from? I don’t have them and the studio swears, I don’t see it in the source code and on the video either
@joshualibrezana3771
@joshualibrezana3771 Жыл бұрын
The " registerReciever" in the MainActivity is error, say Unresolved reference. What should i do?
@el_cucu2512
@el_cucu2512 2 жыл бұрын
great explanation 👍 thanks
@CodeWithCal
@CodeWithCal 2 жыл бұрын
🎉
@gauravkumar-bs3pf
@gauravkumar-bs3pf 5 ай бұрын
how can I implement slide-up animation in this timer?
@mrtruthvirtue8752
@mrtruthvirtue8752 3 жыл бұрын
Thanks for the tutorial! :) What do you think whether jetpack compose will replace all existing xml technology , or not?
@CodeWithCal
@CodeWithCal 3 жыл бұрын
I see a lot of similarities between jetpack compose and SwiftUI. And people still use storyboard and UIKit today, well at least I do 😆 To answer your question, no I don't think it will replace all. But the percentage of apps that use Jetpack will continually increase. What are your thoughts on the matter?
@mrtruthvirtue8752
@mrtruthvirtue8752 3 жыл бұрын
​@@CodeWithCal Thanks again for replying to my question :) I'm a beginner comparing to other guys but I think that android wants to take place among hybrid development kits , frameworks such as flutter , react native..etc. We can use kotlin already to develop ios apps. In my opinion android with kotlin will join the race using jetpack compose so that most of android developers won't have to learn flutter or something.
@fieldget5515
@fieldget5515 2 жыл бұрын
ı thought video is on 2x speed :D Good job my friend
@CodeWithCal
@CodeWithCal 2 жыл бұрын
I've had some pretty mixed reviews on the speed, personally I always liked the straight to the point tutorials. But sometimes I do admit I go a bit overboard with the speed 😉
@marcw6875
@marcw6875 2 жыл бұрын
What exactly is the purpose of the strings in the companion object? Did you make them because some of the functions you would be calling required a string in the parameters and you just needed a string to send? Or do the strings "timerUpdated" and "timeExtra" have some special significance?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
No the string can be anything. I made those names up. The reason its good practice to put them in a companion object. Is the IDE will pick up if changes are made to the variable. but if you have "timeExtra" somewhere then somewhere else you have "timeExtra1" when they need to be the same. well the IDE wont show you any error messages. Did that make sense? short answer is its good practice
@Hrit
@Hrit 2 жыл бұрын
Great Video, but I think we created a lot more functions than we actually needed to.
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Small well named functions are good even if they are only called once :)
@widiprasetiawan7180
@widiprasetiawan7180 Жыл бұрын
How to add micro second?
@alphabetbrumby
@alphabetbrumby 2 жыл бұрын
Thanks Cal. Rotating the screen still causes havok. Do we need to update to and pull from the bundle in MainActivity to fix that ? I'm trying to get a perfect timer going but its a lot harder than expected ! Thanks again for your content, I subscribed.
@CodeWithCal
@CodeWithCal 2 жыл бұрын
kzfaq.info/get/bejne/feCnf9KTu9u8iH0.html This is a swift and xcode but it will teach the principals of the best way to do this. Save a date to shared preferences then compare the start time to now :)
@calvinusta4941
@calvinusta4941 2 жыл бұрын
Nice video! However, I am having an issue at 3:34. Android Studio doesn’t recognise the scheduleAtFixedRate function. Could it be possible it is no longer from the util package that is imported or that it has changed name? I am also having the same trouble with the cancel function at 4:16. Could you help me please?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Check you import is the right class :)
@calvinusta4941
@calvinusta4941 2 жыл бұрын
@@CodeWithCal thanks fir the quick answer! I did import the same 4 packages that you imported, and I also imported more packages trying to troubleshoot this problem
@MuhammadHamzah
@MuhammadHamzah 2 жыл бұрын
I want a countdown, for time being I've done 24 hrs timer which is working fine. What I want is when I close my app the timer should continue working. Does it work with services which you've showed in this tutorial. Or there is some other way to achieve this?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
So I only learnt this recently but you can save the start time to shared preferences and then compare the current time to the start time. Pretty clever not my idea but I've been meaning to make a video on it :)
@heeDeer
@heeDeer Жыл бұрын
Hello its alil bit late but when i type timeTV.time the time was underlined red indicating an error and it shows "variable expected" how do i fix this
@joshualibrezana3771
@joshualibrezana3771 Жыл бұрын
Hello, that's my problem rightnow how did you fix it?
@theosalvadorpereztovar2841
@theosalvadorpereztovar2841 3 жыл бұрын
Nice! But I have the problem when I press Stop, the time sets to 00:00:01, and I don't even use the resetTimer function. Any ideas?
@CodeWithCal
@CodeWithCal 3 жыл бұрын
Is the time updating correctly when you hit start? As in is the error only happening on timer stop. Or could it be an error in the regular expression in makeTimeString 06:40?
@maryannadelman4438
@maryannadelman4438 2 жыл бұрын
Would it be possible to have it make a sound at every 10 minute interval? If so, can you give a code example?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
It's definitely possible. I don't currently have anything to share in terms of sample code. Good luck :)
@konstantinmikhaylov2692
@konstantinmikhaylov2692 Жыл бұрын
so many code for such an easy function which also doesnt really work. glorious.
@casperstockman1799
@casperstockman1799 2 жыл бұрын
Hello! Awesome and intuitive tutorial! I just had one problem, and that is that every time I close my app (/ turn off my phone) the timer stops running after a minute. Do you have any ideas on how to solve this / what I did wrong? Thank you so much! (by the way, the one thing I know I did differently, is that my timer is in a fragment) :)
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Closing the app completely and keeping the timer running. Ive not done something like that personally. Maybe there is a very background thread you could access. But I would guess you would need to ask for some serious permissions to use it
@casperstockman1799
@casperstockman1799 2 жыл бұрын
@@CodeWithCal Wow! That was a quick reply! Thank you anyway! (I'm subscribing!)
@casperstockman1799
@casperstockman1799 2 жыл бұрын
I might try using a foreground service, although I am very unfamiliar with this concept. Do you think this would work?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
​@@casperstockman1799 Yeah looks like that is your best bet. robertohuertas.com/2019/06/29/android_foreground_services/ Have a read of this article i believe this is what you are after :)
@casperstockman1799
@casperstockman1799 2 жыл бұрын
@@CodeWithCal Awesome! Thank you so much!
@someavailablehandlename
@someavailablehandlename 3 жыл бұрын
Hey, great tutorial! Can you please tell me what do I do if I want to keep the service running even when I remove the application from "recent apps"? thank you!
@CodeWithCal
@CodeWithCal 3 жыл бұрын
Is that possible? What use case would you need a process running all the time?
@someavailablehandlename
@someavailablehandlename 3 жыл бұрын
@@CodeWithCal well, it wasn't so much about usecase but just learning. i figured it out tho, I used foreground service. thanks anyway! this video helped
@CodeWithCal
@CodeWithCal 3 жыл бұрын
Nice 🙂
@cynicap8584
@cynicap8584 2 жыл бұрын
hi is there a way I can change this to a countdown instead?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Absolutely, a few to many things you will need to change for a YT comment.
@asadmasood3979
@asadmasood3979 2 жыл бұрын
Great Tutorial! Thanks a bunch for this!! The problem is I get correct duration that is stored in "time" variable whenever I pressed start and pause and then stop. Great?! But when I pressed start and then stop for the first time without pressing pause, the duration returned is 0.0 Any workaround for this?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Not sure I understand the question? What's the difference in your scenario between a stop and a pause?
@MrDrobotor
@MrDrobotor 2 жыл бұрын
Can I somehow use Hours, minutes, and seconds in another function, that's in another class?
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Yes you can. you need to make them public :)
@ethiomusic3158
@ethiomusic3158 2 жыл бұрын
How do I make "Rate Us" and "Send App" in Kotlin language?... I did it in many ways but it doesn't go directly to Google Play Store.... I need help guys!... Right code in fragment... I hope many of you did it if you are app developers by Kotlin
@ioanaborza1876
@ioanaborza1876 2 жыл бұрын
👍
@sergioacedochaidez5772
@sergioacedochaidez5772 2 жыл бұрын
Nice tutorial! I wonder how could i make for every 15 minutos of the timer running to send me a notification or how could i manage the times
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Triggering something every 15 minutes shouldn't be to hard. Do the math of time if time % (15 min) == 0 then call notif code. Here is an article on a simple notification to user stackoverflow.com/questions/5076165/local-notifications-in-android
@giorgi9896
@giorgi9896 2 жыл бұрын
@@CodeWithCal bro u're genius thx
@SergioAndresSierraPayares
@SergioAndresSierraPayares 2 жыл бұрын
Wow such a great video.. I would like to thank u for it. And I wonder.. How to make this example for: Minutes, Seconds and milliseconds?? I've been trying using milliseconds= seconds/1000 and then putting it over the makeString but it didn't work :s
@CodeWithCal
@CodeWithCal 2 жыл бұрын
When we start out timer service, in onStartCommand We say timer.scheduleAtFixedRate(TimeTask(time), 0, 1000) the 1000 is milliseconds. So change this to 1 (meaning call the function every millisecond as apposed to every second) then maybe something like this val hours = resultInt % 86400 / 3600 / 1000 val minutes = resultInt % 86400 % 3600 / 60 / 1000 val seconds = resultInt % 86400 % 3600 % 60 / 1000 val milliseconds = resultInt % 86400 % 3600 % 60 Note this is untested, If it does work id love to know :)
@sundayjacob3945
@sundayjacob3945 2 жыл бұрын
Thank you for the amazing tutorial. I am having an error which is @4.29 seconds. my android studio not recognizing ActivityMainBinding. its showing unresolved rereference: ActivityMainBinding
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Have you enable view binding in the build Gradle?
@heeDeer
@heeDeer Жыл бұрын
@@CodeWithCal i did enable view binding in the build gradle but its still showing unresolved reference for acitivityMainBinding
@Adamos321
@Adamos321 Жыл бұрын
@@heeDeer had the same issue, import line was missing - in import section of MainActivity.kt (in my case the missing line was "import com.example.apstopwatch.databinding.ActivityMainBinding")
@rma1563
@rma1563 2 жыл бұрын
I used Service in a different way, it works fine when screen in on, But when I lock the screen the timer gets delayed, and when I turn on the screen again it seems to work fine. If the phone is connected to a power source everything works fine. The issue arises only when the phone is not connected to power and screen goes off. How to avoid this? 😪
@CodeWithCal
@CodeWithCal 2 жыл бұрын
I recently discovered you can used shared preferences to save the start timestamp. Then you can compare with current date time. Ive done a video on this for iOS. But the principals should be the same :) kzfaq.info/get/bejne/feCnf9KTu9u8iH0.html
@yasserj3693
@yasserj3693 2 жыл бұрын
How can I make it count down
@CodeWithCal
@CodeWithCal 2 жыл бұрын
Should have all the tools, required just give it a start time and then instead of adding a second remove it. You got this! :)
@crttech2902
@crttech2902 3 жыл бұрын
how to make stopwatch using MVVM architecture
@CodeWithCal
@CodeWithCal 3 жыл бұрын
Personally never had much success implementing any architecture systems. I understand this becomes more important the bigger the scope and the more developers working on the same project. But in my experiences it's been more hassle than it's worth trying to stick to theses structures. That doesn't mean they don't have a place and time and that I don't value clean and concise code.
@fernandozhahirsyah7373
@fernandozhahirsyah7373 2 жыл бұрын
After finishing tutorial, i cant open my aplication. Can run but cant open.
@CodeWithCal
@CodeWithCal 2 жыл бұрын
What's the error in your console?
@ricardohenrique920
@ricardohenrique920 Жыл бұрын
The code?
@CodeWithCal
@CodeWithCal Жыл бұрын
Should be in the description
@alinorozi1408
@alinorozi1408 3 жыл бұрын
Like
@bilgipaylasmplatformu8454
@bilgipaylasmplatformu8454 3 жыл бұрын
how to make foreground service never stop
@CodeWithCal
@CodeWithCal 3 жыл бұрын
🤔
@casperstockman1799
@casperstockman1799 2 жыл бұрын
Hello! What worked for me, was saving the paused time in shared preferences then going back and subtracting that time from the current time, if that makes any since (I assume you want the timer to continue when the app is closed).
@HIN30N
@HIN30N 2 жыл бұрын
So fast 😛
@CodeWithCal
@CodeWithCal 2 жыл бұрын
😉
@user-bj4ov2bt3i
@user-bj4ov2bt3i Жыл бұрын
6:00 falta crear la getTimeStringFromDouble
@mohamedabdelbaset578
@mohamedabdelbaset578 Жыл бұрын
not clear at all very fast and write code without explaination
@CodeWithCal
@CodeWithCal Жыл бұрын
Thanks for feedback
@KingswayTradesToo
@KingswayTradesToo 3 жыл бұрын
Nice!!! But, you're too fast, it's too hard to follow up
@CodeWithCal
@CodeWithCal 3 жыл бұрын
Thanks for feedback ☺️
@michaelk.jensen1611
@michaelk.jensen1611 Жыл бұрын
Your time calculation is very and uneccesarily expensive the, modulo operator is arbitrarily costly depending on the time, at least save the first modulo calculation and then use that as base for the minutes and seconds. i would suggest.
@CodeWithCal
@CodeWithCal Жыл бұрын
Sounds good
@IgorogI1000
@IgorogI1000 Жыл бұрын
my honest reaction is WTF
@mrtruthvirtue8752
@mrtruthvirtue8752 3 жыл бұрын
to add millisecond to the stopwatch :) this is the method I've learned from you :) timer.scheduleAtFixedRate(TimeTask(time),0,10) val result = time.roundToInt() val hours = result % 8640000 / 360000 val minutes = result % 8640000 % 360000 / 6000 val seconds = result % 8640000 % 360000 % 6000 / 100 val milliseconds = result % 8640000 % 360000 % 6000 % 100 private fun makingTimeStringFromTimeVariables(hours: Int, minutes: Int, seconds: Int,milliSecs : Int) : String = String.format("%02d:%02d:%02d:%02d",hours,minutes,seconds,milliSecs)
Understanding and Using Services in Kotlin - Beginner
16:08
Android Geek
Рет қаралды 11 М.
WORLD'S SHORTEST WOMAN
00:58
Stokes Twins
Рет қаралды 126 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 117 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 55 МЛН
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 7 МЛН
Tic Tac Toe Android Studio Kotlin Tutorial | Noughts & Crosses
9:47
Stopwatch in Android Studio using Kotlin | Beginners Projects
21:35
Android Knowledge
Рет қаралды 2,7 М.
Bound Services on Android
43:00
CodingWithMitch
Рет қаралды 61 М.
We Built an App in 24 Hours [No Code]
11:56
Raw Startup
Рет қаралды 418 М.
Color Picker Android Studio Kotlin Custom Spinner Tutorial
9:39
Code With Cal
Рет қаралды 8 М.
WORLD'S SHORTEST WOMAN
00:58
Stokes Twins
Рет қаралды 126 МЛН