 Today morning I took your first session and fortunately I am taking your last session also now I am taking android components as Nilesh has shown you the basic components the basic android application what is an android application how to run it what are text box what are labels then also I will explain it another time slowly. So if you are not able to if you know were able to follow at that time you can follow now what all I will be covering in android components basically there are four types of components first is activities second services third broadcast receivers and fourth is content providers we will be seeing one by one the how to implement it programmatically as well as application component as I said there are four types of application components first is activities what does activity mean it is a visual user interface focused on a single thing a user can do services unlike activities a service does not have an user interface it means that a user cannot see in front of his screen services they run in the background third is broadcast receivers broadcast receivers receive and react to broadcast announcement the fourth is content providers it allows the data exchange between applications. Now I am going in detail what is an activity what is an activity an activity means a single screen with a user interface an application usually consists of multiple activities that are loosely bound to each other an activity generally provides a user with an interactive screen to do something like dialing the phone weaving a map etcetera every screen in an application is an activity by itself suppose in any of the android phone what you see in front of you is itself an activity what you push what you press another button it indirectly is an activity by itself let us take an example of an activity let us take an example of an email application an email application might have one activity that shows a list of new email we can have an another activity that shows to read an email although the activities work together to form a user interface in the email application each one is independent of the others means all the activities are not connected to each other for example if we have a camera application camera application can start the activity in the email application if we want to compose a new mail suppose if the user want to share a picture then he can do it intents what are intents intents would be used in the programs that I would be showing you so what is an intent intents are used as a message passing mechanism that works both within an application and between the application there are two types of intents the first is implicit as well as second is explicit let us talk about intent implicit intent implicit does not specify a component instead it includes enough information for the system to determine which of the available component is best to run it look just look at the course nipet there is one object created by intent and in the URI we are passing the address of Google and in the final line we are starting an activity by passing web intent as an argument this means that on really cooperate we will be redirected to the Google website some examples of implicit intents are launching a Google map opening a SMS application or like we do in everyday life making a call making a call is also an implicit intent so what is an explicit intent explicit intents specifically defines the component which should be called by the Android system suppose we are in one application and we want to call another applications class that we can do it with the help of explicit intent the course nipet is as follows intent the same thing but we are specifying the name of the class in the argument and that to class file and last we are starting an activity some of the examples of explicit intents are to start an activity or service to perform an action or to broadcast that an event has occurred about the broadcast broadcast I will talk you later but just know now we will look at the program demo I have written the program before only I am just showing it first of all let me show what the output would be like what is the output of the program so that you can know what the program does now I am running the program by right clicking on the project and running as run as Android application this is the emulator this is the application that is one label two labels which is written activity one and the second is enter your name and there is one text box and one button the name of the class file is main activity we are in the main activity again I am repeating we are we are having two labels one is activity one and the second label is enter your name and there is one text box corresponding to it and a single button so in the text box I would be writing some name and clicking on submit I am writing Akash and clicking on submit what it does like now we are in activity two that is one label welcome and Akash whatever we have written in the text box that name is transferred to another activity through intents I click on back and again I am redirected to the first activity this means like through intense intent we can move between the activities now I will show the coding part these are the two files main activity as well as screen to as and their corresponding XML file XML file contains the design part and main activity and screen to contains the logic part I click on main activity dot Java I have declared two button a single button and an edit text I have an important method add listener on button whatever if a user clicks on the button this full block of code would be executed so on the click of the button on click listener the following three lines of code would be executed what does this three line indicate as you have seen I have mentioned in the PPT like I have used intent so that intent I am using here as you know that explicit intent is used to call the internal class files so I am using here an explicit intent in the intent I am passing screen to dot class as an argument so whenever this line executes screen to dot class we are redirected to intent dot put extra means whatever we have entered in the text that text is given to the activity number two so that we can receive the message activity one passes the value to activity two and finally we are starting the activity passing intent as an argument now just look at screen to that is activity two in activity one we have send a message and activity two should receive the message here I have declared a title message and button title gets the screen gets the message from the activity one and message just displays the that the message would be got from activity just to display in activity two so title gets the message and message prints the message label so in the back button also as you know I have declared one another button in activity two that button I am redirecting again to main activity it means it functions like a back button so I am like navigating between one activity to the second activity and then second activity to the first activity and in that I am declaring main activity dot class and finally start activity passing intent as an object this line is used for opening a new activity again I am running the program so that you would be clear run as right click android application here it is the emulator takes some time first time to start it we are in currently in activity one I am entering the name akash android and clicking on submit button on the click of submit button I am redirected to activity two and within welcome we said akash android akash android that which I entered in the previous activity on click of back I am again redirected to activity one with a null with an blank screen so you would have now understand what is the mean of activity jumping between one activity to another activity and any number of activity just you should call intent internally there are two explicit intent juggling between the main screen in so that is all for activity now I am going to tell about services what are services services also an important android component what is service a task that runs in the background without the users direct interaction means it means that the user does not need anything to worry about the task runs in the background no worry about that a service does not have a user interface means unlike an activity it does not have a screen a service by default runs in the main thread of the application that houses means it runs in the that house it's some of the main good examples of services are network downloads the second is playing music the third is checking updates for an application a service can run in the background to perform work even by the user reason in a different application we can assume that a service does not require any user interface like it just runs in the background no need to worry what the user does or does not do it will itself run it a service might play in the music in the background while the user is in different application like if the user wants to hear to music and want to message so like the music would be playing in the background so this is the best example of services checking updates for application it may happen that on click on when the internet is there and if a new app a new update for an app is launched then it prompts us to download it so this is an example also of a service now we look at the demo program demo of services how services work and what are the code required I have a program name service launcher I am right clicking it and running as android application this is the emulator I am writing the output would be shown here here it is and there is one labeled which states that an application showing the use of the services there are two buttons start button as well as stop button on clicking of start button a message would be displayed in the bottom stating that service has been started and the counter would be at 0 obviously when we start a service the initial where we should be from 0 on clicking of stop service another message would be highlighted here stating that the service has been stopped and the counter would be at some positive number at last it also says that how much time the service was running in the background so between the start as well as stop service and icon would be a label would be placed on the notification bar stating that a service has been started so I click on start service as a message has been displayed stating that a service created at this much time and counter is at 0 now as you look up a label has been added in the top bar notification bar as I pull down it a label is visible service label with an android logo in it I push up there means the service has been started and is running in the background now I brought some other features of the phone but still the notification bar is there and services also running I just randomly enter some number just now I clicked on call button and I am randomly clicking some numbers during the clicking of the number the service is still running in the background as you can see the logo if I want to stop it I pull down on click and click on service label as I click on service label I will be again redirected to the service notification main activity and I click on stop service as soon as I click on stop service a message is displayed stating that a service destroyed and the counter is at 78 in the last message service running for 77 seconds it means that during the browsing whatever I did the service was running in the background now I am stopping the service and the service has been stopped so the upper icon android icon has disappeared now no longer the service is running now let us look at the code this is our code two main methods two main class notify service and service launcher let us look at service launcher I have declared two buttons that you have seen in the main screen start as well as stop on clicking of start service start button this particular line of code would be implemented here here also I am using explicit intent in activity also I have used explicit intent means we are going to another class in stop service also I am using an explicit intent and here also I am going to the main class the main class means that we are having two buttons that is a main class now notify service here the logic is there I have declared notification manager and I am object a timer and couple of two timestamp to know the difference between the start service and stop service after that I have a timestamp in timestamp I am storing the system current time in milliseconds and toast what does toast mean toast means just displaying a message on the screen and displaying a message on the screen stating that a service has been created at this time and counter is at 0 and at the start of the service the counter would be at 0 two main methods are that show notification and increment notification in show notification I have commented all the lines it is also self-explanatory let me read in I am in this particular line we will be using the same text for the ticker and the expanded notification you have seen that what I dragged down this line is used for that and pending intent I will be explaining pending intent in another program because in that it is the major part is that and increment counter I am having a timer with checks every second whether the service is running or not on destroy on destroy if the user clicks on another stop button it goes on on destroy and in that I display a message stating the service has been destroyed getting the counter to some positive integer number at last another message stating the service running for and difference of the time the difference is be calculated between the timestamp in one timestamp I am storing the system dot current time in milliseconds and taking the difference of both now again you would be clear what the program is doing just have an another look of the output of the code right click run as android application sorry the emulator has already been opened so there is an warning stating that activity not started its current task has been there so I just click on the emulator in the sidebar and the and it opens I click on start service it states the service created counter is at 0 icon has been added I pull it down and I bronze any other thing in the phone emulator then I pull down click on service label and I click on stop service service destroyed and the counter is at 23 service running for 22 seconds so you would be clear what the service is like the third component is android broadcast receivers what is a broadcast receiver broadcast means saying yeah I am available for update some system says like if you have a android phone if the battery is low there would be a message stating that battery is low please connect it to recharge means it is telling the message to all the applications a broadcast receiver is a component that responds to the system wide broadcast announcements many broadcasts originate from the system itself for example the screen has been turned off the battery is low or a picture has been captured or an SMS is received applications internal applications can also initiate broadcast for example to let other application know that that some data has been downloaded to the device and is available for them to use like if we install some application from Google play if we install some games after downloading of the game it stays let your game has been downloaded your you can play it is also some it is also one of the kind of broadcast receivers it creates a status bar of notification to alert the user when a broadcast event occurs means that as I shown in the top in the top of the emulator and label has been added it shows that it alerts the user that a broadcast event has occurred an example would be that although broadcast receivers do not display a user interface just like a service it does not indicate any user interface UI they may create a status notification to alert the user when the broadcast event occurs broadcast receivers are also activated by intents but here we will not be using implicit intent explicit in the we will be using implicit intents now we will be looking at the program code this is a program code first of all let me run the program right click run as android application the emulator has started here we have a two buttons send notification and cancel notification send notification sensor notification to all to all the application that it has some message to tell cancel notification tells that cancel notification we are cancelling it as I click on send notification a new label is added and as I roll it down a label is there stating notification details browse android official site by clicking me and on clicking of it I would be redirected to the android official site cat should be taken that your PC is connected to the internet otherwise page not found would be there when I cancel notification this above label would disappear I click on cancel and at the same time the label has disappeared now let us look at the code there is only one file simple notification or Java I have declared two buttons start as well as cancel this is the main code on clicking of start button the following code would be executed here new thing used is pending intent and here another new thing is that we are using implicit intent in the previous two examples we have been using explicit intent to juggle between activities now we want to go beyond activities and fetch some other data from the go to some other website in the internet. So, for this we are using intent that to implicit intent and inside the argument we are giving the URI of the website here I have given android.com slash about another thing is pending intent what is a pending intent of pending intent is a token that you can give to any foreign application like Alara manager notification manager etc which allows the application foreign third foreign or third party application to use our application permission to execute a predefined piece of code see as you look that we have used here implicit intent the previous two examples we have explained I have we have used explicit the cancel button I am canceling the notification notification manager dot cancel another again as you again I am running the code right click run as application I click on send notification a new alert click me a label has been added I pull down then I click on cancel notification as I pull down a label is there notification details I click on it and I am again redirected to android.com slash about if you are doing this in your college make sure that your college does not have any proxy settings if at all your college has proxy settings you should provide the proxy settings in the emulator it is taking some yeah here it is here we have open ww.android.com slash about about so you would have now get the got the difference between an implicit as well as explicit intent explicit starts an activity using is class name while implicit makes a call to another third to another third party like fetching a website through internet explicit does not require any internet connection but implicit intense do require so make sure you are connected to the internet otherwise you will be welcome to the message paid not found now the final component is content providers what is a content providers it is a shared set of data it makes some of the application data available to another application it is the only way to transfer data between applications in android unlike the other other other was like in any other language it does not have any shared files it does not have any shared memory or pipes etcetera these all things are used to transfer message or data in other languages so like in android there is only one method to get data or send data that are content providers a content through the content providers other application query or even modify the data we should make sure the content provider allows it to do we should have the required permissions in the XML file for let us take an example for example the android system provides a content provider that manages the users contact information typically a provider is backed by an SQLite database a SQLite database in a open source database which is embedded inside an android let us take an example stating that we have an our own application name app one we have another application app two which uses an internal database of contact information so if the app one wants to take some data from the app two it requires a content provider so it is a bridge between a single application to another application for getting the data the best example is like on our android phones we would be having the contact information suppose that contact information we want to use in another application in built our own application we would be using content providers the practical part of content providers will be given tomorrow so I am like you should be clear about all the three building blocks