Core Building Blocks of Android –
Component in Android –
In the android component is an already pre written code which has a life cycle.
The Core building blocks include these components. In which the Android Applications can be built. In android Core building blocks are fundamental components. Android Developers should understand these components.
Fundamental components of Android are –
Activity, Intent, Service, Content Provider, Broadcast Receiver.
Activity is a screen . Activity is the main Screen which contains UserInterface(UI) and Logic .Contains XML and java code.
Activity file contains two files Activity.xml which is used for UserInterface(UI).and second file contains java file (Activity.java) used for writing Logic .
Intent is a message passing object. If you want to communicate from one activity to another then we can go for intent. Intent also helps to communicate between other components like service or broadcast receiver.
Use of intent is –
- Start Activity.
- Start Service.
- Delivering broadcasts.
- Opening a webpage.
There are two types of intent –
- Implicit Intent
- Explicit Intent
To find Best Android Training in Pune.
Overview of Services In Android-
Services is an android component that can perform long-running tasks in the background. Service doesn’t have any user Interfaces . Because service runs in the background. Service runs only on another application . Service cannot start by itself .Android service is not a thread or separate process –
Examples of services are –
- Music player which plays music in the background.
- Network transactions Applications.
- Content provider which does database related tasks in the background.
- Performing file input and output
Above are very common examples of services.
To find Best Android Classes in Pune.
Life cycle of service –
When service gets started with startService() method then its life cycle starts . service will run in background until it is stopped by stopService() or stopSelf() method. Service Always runs on the main thread.
1)Background service: A service that performs an operation that is not directly noticed by the user.
2)Foreground service: A service that performs a certain operation that is noticeable to the end user is called foreground service.
To find Best Android Course in Pune.
Example is : Playing audio track. we need to close the services to stop service. Foreground service must display notification.
Differences between Service and Thread –
Service: Service runs on the main UI Thread. Service is a component of android. Service performs a long running operation in the background. Generally service does not have any UI.
Thread : Thread is an operating System level feature that performs some operation at Background.
Difference between Thread and Service
Thread : Thread is not an android component. If the thread gets destroyed, Android will not restart the thread. For this reason Users may lose their data.
Service: Service is an android component. Android handles it. Because service has its built-in component. The Service has priority levels in android . For doing some background tasks better to select a service. Above are some differences between thread and service.
Let us see the Steps for creating a service in Android –
- To create a service. Service needs to be declared in AndroidMenifest.xml .
- Extend a class with Service. When we extend with a Service class we get two methods.
public int onStartCommand(Intent intent,int flags,int startId)
The above method returns int value which defines the restart behavior when service gets terminated by android platform.
onstartCommand() is used to start a service .
To stop a service we have to use the stopService() method . This will kill the service.
Sometimes we want to stop service by itself, in that case we can go for the stopSelf() method . This method stops service when service finishes its task.
stopSelf() is also used if service finishes its task.
Lets us discuss the different call back methods of service
While creating a service we can override certain methods like below. We have already discussed OnstartCommand() , stopService() and stopService() methods .
System invokes this method to perform ontime setup. When service is created . if service already runs this method will not get invoked.
when service is no longer in use. Then this method will destroy the service.
Public IBinder OnBind(Intent intent)
This method binds to the services, sends requests ,receives responses and performs inter process communication(IPC). Abound service typically lives only while it serves another application component and does not run in the background indefinitely.
After discussing the Core building block of Android . we will discuss choosing Language in android studio.
Choosing Programming Language in Android Studio –
While creating App using Android studio(Android studio is Integrated Development Environment for creating mobile App. Which is specially designed for creating only mobile App.) we need to choose a language in Android studio.android developers can choose kotlin or java. As for writing the logic for the App.
Android Developer’s can have an option to choose the language for writing app logic.
Android developer’s can choose java language or kotlin language.
Let us discuss What is Kotlin?
Kotlin is programming Language for JavaVirtual Machine(JVM). Android Developer’s can use the kotlin language for development. Apart from mobile App Development .We can use kotlin for client/server programming also. Kotlin Language is developed by JetBrains. Currently kotlin is predominantly used for Android App Development.
Kotlin is more concise than java. Kotlin has inbuilt null safety. Kotlin can solve Null pointer exception . Null Pointer exception is a commonly occurring exception that developers must handle. In android null means nothing or to represent absent value. Mostly it is used to represent null value. When exception occurs in application. It is the role of a developer that must handle exceptions . Using Related exception handling techniques.
In Kotlin language inbuilt features for null safety. Since Kotlin language has a built-in feature for handling null pointer exceptions, development becomes faster, because the language itself takes care of exception handling so android development becomes faster.
Kotlin code development speed is fast as compared to java. Kotlin supports lambda expressions . In which anonymous methods (functions) can be treated as values. You can pass parameters to methods, any logic that you can implement. For community support java has a very large community. Kotlin has very small community. Kotlin is interoperable with java.
Android studio has excellent support for kotlin. Once we install kotlin plugin . Kotlin supports Lambda expressions . Java 8 onwards only Java supports lambda expressions. Adapting kotlin concepts is very easy. From a performance point of view kotlin is slower than java.
Android development is always done with java.. Because of simplicity , performance and maintainability robustness.kotlin code is lightweight.
Language than java.kotlin add some modern features like
- Kotlin can build code that runs on ios also.
- Kotlin is Statically typed and open source language.
Lets us see Top Companies using Kotlin for Development
Uber,Gradle,and Pivotal are some companies using Kotlin.
Here is a discussion about kotlin and java –
Kotlin Language is developed by Google as a First-Class Language supported on android. While doing Mobile App Development Kotlin is Preferable than java. Kotlin is code safer Language . In kotlin documentation is very well implemented. So Conclusion is that While android Development we can choose Kotlin.
To know interview Questions on Android you can refer a link.
SevenMentor Pvt Ltd
Call the Trainer and Book your free demo Class for now!!!
© Copyright 202! | Sevenmentor Pvt Ltd.