Тёмный

StopWatch Timer on Service Android Studio Kotlin Tutorial 

Code With Cal
Подписаться 11 тыс.
Просмотров 27 тыс.
50% 1

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 91   
@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 :)
@konstantinmikhaylov2692
@konstantinmikhaylov2692 Год назад
so many code for such an easy function which also doesnt really work. glorious.
@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 2 года назад
@@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")
@ousaidanass1383
@ousaidanass1383 3 месяца назад
Thank you so much
@joshualibrezana3771
@joshualibrezana3771 Год назад
The " registerReciever" in the MainActivity is error, say Unresolved reference. What should i do?
@casperstockman1799
@casperstockman1799 3 года назад
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 3 года назад
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 3 года назад
@@CodeWithCal Wow! That was a quick reply! Thank you anyway! (I'm subscribing!)
@casperstockman1799
@casperstockman1799 3 года назад
I might try using a foreground service, although I am very unfamiliar with this concept. Do you think this would work?
@CodeWithCal
@CodeWithCal 3 года назад
​@@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 3 года назад
@@CodeWithCal Awesome! Thank you so much!
@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.
@hlkihglkhglkhg
@hlkihglkhglkhg 2 года назад
You sir are a legend, thank you!
@CodeWithCal
@CodeWithCal 2 года назад
😁
@gauravkumar-bs3pf
@gauravkumar-bs3pf 7 месяцев назад
how can I implement slide-up animation in this timer?
@fieldget5515
@fieldget5515 3 года назад
ı thought video is on 2x speed :D Good job my friend
@CodeWithCal
@CodeWithCal 3 года назад
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 😉
@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
@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 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-GzwLobVuXXI.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 :)
@MuhammadHamzah
@MuhammadHamzah 3 года назад
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 3 года назад
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 :)
@happyroden8826
@happyroden8826 3 года назад
thank you, good video explanation, i've subscribed
@mohamedabdelbaset578
@mohamedabdelbaset578 2 года назад
not clear at all very fast and write code without explaination
@CodeWithCal
@CodeWithCal Год назад
Thanks for feedback
@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 :) ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-GzwLobVuXXI.html
@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
@widiprasetiawan7180
@widiprasetiawan7180 Год назад
How to add micro second?
@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 :)
@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
@el_cucu2512
@el_cucu2512 2 года назад
great explanation 👍 thanks
@CodeWithCal
@CodeWithCal 2 года назад
🎉
@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?
@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
@KingswayTradesToo
@KingswayTradesToo 3 года назад
Nice!!! But, you're too fast, it's too hard to follow up
@CodeWithCal
@CodeWithCal 3 года назад
Thanks for feedback ☺️
@SergioAndresSierraPayares
@SergioAndresSierraPayares 3 года назад
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 3 года назад
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 :)
@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 :)
@heeDeer
@heeDeer 2 года назад
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?
@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?
@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?
@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.
@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.
@jerichosantonia6412
@jerichosantonia6412 2 года назад
thanks man!
@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
@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! :)
@PauGalera
@PauGalera Год назад
6:00 falta crear la getTimeStringFromDouble
@bilgipaylasmplatformu8454
@bilgipaylasmplatformu8454 3 года назад
how to make foreground service never stop
@CodeWithCal
@CodeWithCal 3 года назад
🤔
@casperstockman1799
@casperstockman1799 3 года назад
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).
@ricardohenrique920
@ricardohenrique920 Год назад
The code?
@CodeWithCal
@CodeWithCal Год назад
Should be in the description
@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
@ioanaborza1876
@ioanaborza1876 2 года назад
👍
@HIN30N
@HIN30N 2 года назад
So fast 😛
@CodeWithCal
@CodeWithCal 2 года назад
😉
@IgorogI1000
@IgorogI1000 Год назад
my honest reaction is WTF
@alinorozi1408
@alinorozi1408 3 года назад
Like
@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)
Далее
Calculator App Android Studio Kotlin Tutorial
12:22
Просмотров 77 тыс.
Women’s Goalkeepers + Men’s 🤯🧤
00:20
Просмотров 898 тыс.
RecyclerView, Adapter, Model Часть 1
13:12
Просмотров 10 тыс.
Everything Starts with a Note-taking System
21:23
Просмотров 254 тыс.
Foreground Service в Android Studio/Kotlin
17:50
Bound Services on Android
43:00
Просмотров 62 тыс.
Building a Mobile App in 2024: The BEST Technologies
13:31
Women’s Goalkeepers + Men’s 🤯🧤
00:20
Просмотров 898 тыс.