AlarmManager in Android Studio || Notification using AlarmManager is Android Studio || 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox Android so in this video we're gonna learn how to implement alarm manager into our Android application right so those who does not know what is alarm manager so basically it is a feature that Android SDK provides us if we want to run a particular code at a given point of time no matter if our app is running or not that particular piece of code will run at a time that we have set right so let me just give you a brief pronunciation of what you're gonna get by the end of this video so if you can see on my mobile screen I have this three buttons select time set alarm and cancel alarm so if I just tap on the select time and select 10 35 PM okay so if I just set the alarm so you can see I have successfully set the alarm for 10 35 PM so let us just wait for that time so he can see we have got the notification right if I just swipe down and click on this notification I go to this activity right so this is what we'll be getting by the end of the video if you want to implement the same to your Android application make sure you watch this video till the end so without what you do let's get started [Music] so guys if you're new to this channel make sure to hit that subscribe button press the Bell notification icon for saving notification of the upcoming videos and by the end of the video if you like the video make sure to hit that Thumbs Up Button as well so let's start with the development so very first thing we are going to do is we are going to make the front end parts so let me just delete this text View and let us just go to split window will set the root layout as linear layout and the orientation is already vertical so let me just quickly Design This front end part then I'll let you know what we have done right so guys we are done with the front end part so basically we have a text view where we will set the time that is being selected by the user then we have three buttons to select the time to set the alarm and to cancel the alarm so this is quite a simple layout right uh there's nothing fancy about it that should be explained so now let us just quickly dive into the backend code so before just going there we will enable The View binding feature so for that I'll just come to this build.gradle app level build.gradle right and here we'll enable build features and view binding as true let's just sync the project so okay this Inc has been completed now we'll just go to main activity so now as you know that this feature is enabled if even if our app is not running right if it is not running then also we'll get this notification right so for that we need to create a broadcast receiver that will receive the notification at that particular time so for that we'll just create a new Java class so just right click here new Java class and I'll just name it as alarm receiver okay so this class will extend the broadcast receiver class right so now we need to implement a method so Alt Enter Implement methods there is only one method on receive so whenever this particular notification is being pushed right this particular code will run the code that is inside this on Save method will run so here we'll make a notification Builder and will show the notification but one more thing we need to do is we need to specify this broadcast receiver in our and Android dot manifest file so let us just quickly do that so for that I'll just come inside this application receiver dot alarm receiver and that is it we have set this broadcast receiver make sure you do that here right otherwise this will not work so now inside this I'll just create a notification right and let me just quickly do that so guys we have created a notification right here you can see I have just created a notification I have set certain attributes here you can see I've said the channel ID we'll just create this channel ID this is used in Android level 8 and above right you need to create a notification channel that will create in the main activity in just a while right and then we have set certain attributes so let me just brief you about it set small icon the icon that will be shown when notification is displayed then we have the title the title of the notification then we have the description that will be shown in smaller text set auto cancel as true means when the user pass on that notification it should be uh canceled right it should be erased from the notification country then we have set all the defaults then set priority is equal to high means that what level of priority is there of your notification right for example priority High means it will be shown even on the lock screen or it'll just pop up from the top when the user is using his cell phone right you can set various priorities priority low priority default I'll just put the link for the official documentation of notification in the description below after this video you make sure you check out that documentation everything is explained in a clear manner that word priority needs to be set for a particular application right you can just go through that documentation and then you can change this attributes according to your application right so then we have create the object of this notification manager compact right and then we have called this dot notify basically this will show the notification but when this notification is shown this will not do anything if user Taps on it but we want to make sure that a new activity is opened up if a user Taps on this application so for that we'll create an intent so here we'll pass the activity that we want to open right so for example I have just empty activity destination activity right so I want to open this activity so I'll pass this so in 10 dot set Flags okay we have created a pending intent right where we have passed this intent as an argument then we have the context request code and the flags now if we want that when the user Taps on it this activity should open up then we should to set content intent and we should pass this spending into it so that is pretty much everything for this broadcast receiver now let us just go to our main activity and do our thing so the very first thing as usual we do is debut binding thing so okay now we'll just set the on click listen around on all the three buttons right select time Dot so okay we have set the unclick lesson on all the three buttons so the very first thing we are going to do is we are going to create the Channel that I was just talking about that is this channel right this notification Channel we need to create so for that let us just create a method create notification Channel let's just create this method so this notification Channel needs to be created if your app is running on Android Oreo or above right so for that we'll just check if Bill Dot version dot SDK is greater than okay is this particular code will create a notification Channel this needs to be done in this way only right so we have set the name of the channel make sure you set the name of the channel for whatever reason you are making it for example you are making a texting app or a Facebook app so there are various channels if you just look into the notification tab in the settings of the Facebook for example for if you receive a friend request there is a different channel for that if you receive a text message there is a different Channel you can create as many Channel as you want so the name should be according to the application of the channel and the description should describe for what you have made this channel this importance again is will do the same thing it will set the importance of of the notification and it'll behave accordingly for example if I have set importance high that means it will be shown even if the screen is locked and it will be popped up when the user is using his cell phone right and here you can see we have created this Channel and the ID of the channel shouldn't match with this one make sure this these both things match with each other so let me just copy this it should be a same name right then we have passed the name importance and we say the description and then we have got this notification channel right so there is nothing much to understand about it just understand this is important and this channel ID is important right so when the user Taps on this select time button right we are going to call a method show time picker so let's just create this method so inside this show time picker we want to show the time picker right so for that will just create material time picker and we'll just name it as Speaker only so let me just write down the code quickly so guys we have implemented this Showtime picker right we have got the instance of the material time Baker we have created object for that we have set the initial time when time minutes and the title when this speaker will be shown right then we have called this speaker dot show which will show the Picker then we have set the positive button click listener that means whenever user presses on that ok button what needs to be done so this particular line of code will set the time in the text view that we have implemented in our layout right and then we have got the instance of the calendar and we have set the hour of the day to the r that users selected in the Picker then the minute to the minute selected by the user seconds to 0 and milliseconds to zero as well right so basically we have got the time inside this calendar instance right now our calendar will point to that particular time of the day what user have selected from the Picker this will be used to set the alarm so now inside this set alarm button we'll just call a method set alarm let us just create this method now and let us create a reference variable for alarm manager we need to cast it let's just use this we'll create an intent and we'll just also create a pending intent so private so okay you can see alarm manager dot set basically this set will set an alarm just for once right this alarm will be turned on once for example if just set it for 5 PM it'll be turned on at 5 PM that is it set in exact repeating means that you want to repeat it at a particular interval that will you will pass inside this method right but in exact repeating means what that for example uh when I was just demonstrating it I set it for 10 35 but it got turned on at 10 36 right that means one minute after the time I have set it so this this particular things will not turn it on at the exact time that you have set in but a minute earlier or a minute after the time you have set right so basically this enhances the performance of your device right so if it is not that urgent you can just bear with one minute delay you should always use set in exact repeating because at the back end Android SDK just combines all the uh alarms that has been called in between that that time period and clubs it and set up broadcast receiver to all the applications right so if you want to use a exact time then you should use this set exact right but for this point of time I'll be using set in exact repeating and first thing I'll pass is alarm manager Dot so here you can see we have got four options RTC wake up RTC collapse real time elaps real time wake up so at RTC means real time clock Labs means when it'll just considered the time when the system or that that particular device was booted right so we will be using the airtime clock real time clock is the actual time that is being there in the universe and if you just want to trigger something based on the the time at which the device was booted then you should use this elapsed real time and RTC wake up means is it will just wake up the device at that particular time right to show this notification and simple artists you will not do that so we'll use RTC wake up and calendar dot get time in release will pass the time at which we want to turn on this alarm so alarm manager dot what will be the interval for example I want to turn it after uh 15 minutes half day half hour interval of R we want to make it operational at a interval of a day so I'll use that so you can also use the custom repeating so instead of in exact repeating you can set custom if you do not want to set in these five options right for example you want to turn it on after four hours then you should use the custom repeating alarm right so alarm manager dot interval d and the last thing will pass is pending intent so okay we have implemented this set alarm as well this will set the alarm so let me just look at this because I'm using kotlin these days so I forgot to put this semicolons okay we have implemented the set alarm as well so the last method we are going to implement is the cancel alarm so for that let me just call a method called cancel alarm right let's just create this method real quick so for this cancel alarm we'll be needing this pending intent let me just copy it from here to save some time let me just copy this one thing okay now we'll check if alarm manager is equal to null then alarm manager is equal to get system service on text Dot alarm service just type question okay this is the way to cancel the alarm you need to create a pending indent but make sure that this spending intent should exactly match with the intent that you have used here right for example the request code the flags everything should match so you simply copy this here right and you should this particular thing will refer to this thing and then if for example I've used this code because if the user has set the alarm then cancel the alarm this will not point to null right but if the user has set the alarm close the app then again turn on the app it will point to null so we have just got the reference for this alarm service then we have call this cancel function and we have passed the pending intent inside it and we have raised the toast so I guess that is pretty much everything we need to do to implement this complex thing so I know this is a bit difficult to digest but once you implement it into your application then you'll start understanding these things and one more thing you should definitely refer to the official documentation for implementing this thing right I have just given you a basic idea that will make this thing work but for more features or more things to play around with you should definitely refer to the official documentation I'll provide the link in the description so now let me just quickly install this app to my cell phone and check if it is working fine or not right so guys the app has been installed to my cell phone as you can see on my mobile screen so just before testing it we have got two issues when I install this app I've just rectified it the very first thing was I have set the on click listener on the text View and I've just selected this wrong uh ID right the selected time button needs to be selected I have just rectified this and the second thing was these brackets there was some issue with these brackets right make sure that you keep this like this only right or you can just refer to the source code I've put in the link in the description below you can just get the source code and test it right these were two things now let us just test our app so let me just select the time it seven maybe and set the alarm let's just close this app and wait for the alarm to be get triggered right so here you can see we have got the notification if I just tap on this we land to the destination activity right so I guess we have successfully implemented this feature to our Android application and I also hope that you guys have understood the code in case of any route you can always ask me in the comment section or you can DM your Instagram the Instagram username will be there on the screen right now if you like the video make sure to hit that thumbs up button in case you're new to this channel make sure to hit that subscribe button press the Bell notification icon for saving the notification of the upcoming videos so that is it for today's videos see another video bye thank you [Music]
Info
Channel: Foxandroid
Views: 14,304
Rating: undefined out of 5
Keywords: android studio, android tutorial, android alarmmanager, android alarmmanager exact, android alarmmanager tutorial, android alarmmanager broadcastreceiver example, android alarmmanager example, android alarmmanager notification, android broadcast, android broadcastreceiver tutorial, android broadcast receiver, alarm manager, alarm manager android, foxandroid, notification using alarm manager, java, alarm manager java, android studio tutorial, android alarm, 2023
Id: 5RcDWnNgkQg
Channel Id: undefined
Length: 25min 32sec (1532 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.