Core Building Blocks of Android

  • By Bhagyashri Sangam
  • February 21, 2020
  • Android
Core-Building-Blocks-of-Android

Core Building Blocks of Android

Component in Android

In android, the component is an already pre-written code that has a life cycle. The Core building blocks include these components. In which the Android Applications can build. Core building blocks are also known as the fundamental component. Understanding the fundamental Component is important for Android Developers.

Fundamental components of android are:

Activity, Intent, Service, Content Provider, BroadcastReciever.

Activity

Activity is a screen. Activity is the main Screen which contains the User Interface (UI) and Logic. It contains XML and java code. The activity file contains two files Activity.xml which is used for User Interface (UI) and the second file contains java file (Activity.java) used for writing Logic.

Intent

The intent is message passing object. If you want to communicate from one activity to other then we can go for intent. Intent also helps to communicate with other components like service or broadcast receiver.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Uses of intent 

  • Start Activity.
  • Start Service.
  • Delivering broadcast.
  • Opening a webpage.
  • There   are two  types of intent  
  • Implicit  Intent: 
  • Explicit Intent

Overview of Services In Android

Services are an android component that can perform a long-running task in the background. Service doesn’t have any user Interfaces. Because service runs in the background. 

To start a  service in android another application is required.  Service cannot start by itself. Android service is  not a thread or separate process

Examples of services are:

1)Music player who plays music in the background.

2)Network transaction Applications.

3)The content provider does database-related tasks in the background.

4)Performing    file input and output 

Above are very common examples of services.

The life cycle  of service 

When service gets started with the startService()   method then its life cycle starts. service will run in the background until it is stopped by stopService()  or stop self() method.

Bound service is another component that binds the service. Using bindService()  method.

Service  Always runs on the main thread.

We  can create   two types of  services in android

1)Background service: A  service that operates that does not directly notice by the user.

2)Foreground service:   A service that performs a certain operation that is noticeable to end-users is called foreground service.

An example is: Playing audio track.

These services will continue to run until the user closes it.

Foreground service must display a notification.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Differences between Service and Thread

The main difference between thread and service is. Service runs on the main UI Thread.  Service is a component of android. Service performs long-running operations in the background. Generally, the service does not have any UI. 

Thread: thread is operating System-level feature that performs some operation at Background.

The common thing between thread and service is that they both do background operation. But they are not the same.

Let us discuss the comparison between thread and service.

Thread:  Thread is not the android component.   So android will not take thread priority when it comes to low memory. If the thread gets destroy android will not restart the thread.  For this reason, the user may lose his data.

Service:   Service is an android component. In case  If service gets destroyed while performing its job.  Android handles it. Because service its built-in component. For handling the service the Service has priority levels in android. these levels can be considered while destroying service due to low memory.

For doing some background task better to select a  service.

If you want to run a background service as long as your activity is alive. choose the thread with the async task.

Above are some differences between thread and service.

Let  us See  the Steps for creating a service in Android  

  1)To create a  service. Service needs to be declared in AndroidMenifest.xml.

  2)Extend a class with   Service. When we extend  with a Service class we get  two methods 

@Override

public   int onStartCommand(Intent  intent,int flags,int start)

{

//   code

return    Service.START_NOT_STICKY; 

}

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

The above method returns int value which defines the restart behavior when service gets terminated by the 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 stopSelf()   method. This method stops service when service finishes its task.

 stop self()   is also used if the service finishes its task.

Lets us discuss  the different callback methods  of service

While creating a  service we can override certain methods like below.  We have discussed already OnstartCommand() , stopService()  and stopService() methods .

onCreate() 

The system invokes this method to perform the one-time setup. When service is created. if service already runs this method will not get invoked.

onDestroy()

when service is no longer in used.  Then this method will destroy the service.

@Override

Public   IBinder   OnBind(Intent  intent)

{

 return   null;  

}

This method bind to the services, send request, receive the response and perform inter-process communication(IPC). Abound service typically lives only while it serves another application component and does not run in the background indefinitely.

There are two classes of service. 

Service:  this is the base class of service. When we extend this class. it is important to create a new thread in which service can complete all its tasks. This uses the application’s main thread.

We need to stop the service. Using stop Service() method.

Intent Service

Intent service is a subclass of service class. This intent service handles the asynchronous request.

This service is started as needed. Intent service creates worker threads. And uses that thread to run the service. Intent service automatically stops when it finishes its tasks.

After discussing the  Core building block of  Android. we will discuss choosing Language in android studio.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Choosing Programming Language in Android Studio

While creating App using Android Studio(Android studio is an Integrated Development  Environment for creating mobile App. Which is specially designed for creating only mobile App.?)   we need to choose the language in Android studio. It can be java or it can be Kotlin.

As    A Mobile App Developer, you can choose Java or Kotlin.  For coding purposes. The person from java background can choose easily java. But Kotlin is also chosen as an alternative to it. 

Let us discuss what is  Kotlin?

Kotlin is a programming language for  Java Virtual Machine(JVM). So we can use Kotlin instead of java.

Apart from mobile App Development. We can use Kotlin for client/server programming also.

Kotlin Language is developed by  Jet Brains.

Currently, Kaitlin is predominantly used for Android  App Development. 

Kotlin is more concise than java.

Kotlin has inbuilt null safety. Kotlin can solve Nullpointerexception.  Which is a very large mistake in android development? In android, null means nothing or to represents absent value. Mostly it is used to represents a null value. But some cases when we use null in android but it will throw null pointer exception. The developer has to handle that exception every time.

But the same coding is done using Kotlin. It has the inbuilt feature for null safety. So the developer can concentrate more on his coding part. Rather handle the exception. This is the advantage of using Kotlin. 

Kotlin code development speed is fast as compare 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 a very small community.

Kotlin is interoperable with java.

Kotlin is Easy for Learning.

The person from java background can learn Kotlin easily. Java syntax is very complex.  Writing longer code can always have bugs and errors. Kotlin Syntax is very easy. Kotlin is a modern language.

Kotlin has the majority of Java libraries and frameworks. Since Kotlin is the enhancement of java.

Both java and Kotlin’s code can work simultaneously.

In Kotlin developers can extend a class with new functionality using the extension. This type of feature is available only Kotlin not in java. This feature is known as an extended function. 

Android studio is excellent support for Kotlin. Once we install the Kotlin plug-in. Android studio makes a configuration for the Kotlin project.  

Kotlin support Lambda expressions.   Java 8 onwards only java support lambda expressions. 

Adapting Kotlin concepts is very easy. 

From the performance point of view, Kotlin is slower than java. 

Android development is always done with java. But now  Android development is replaced with Kotlin. Because of simplicity, performance and maintainability robustness. Kotlin code is lightweight Language than java. 

Kotlin adds some modern features like Compiling code to JavaScript or Native Script.

Kotlin can build a code that runs on ios also.

Kotlin is a statically typed and open-source language.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Top Companies using Kotlin for Development

Uber, Gradle, Pivotal are some companies using Kotlin. 

We will discuss which is the better option either java or  Kotlin.

Kotlin Language has been announced 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. Because  Kotlin prevents common programming mistakes by design.

In Kotlin documentation is very well implemented. It is better than java like Syntax, compatibility.

So Conclusion is that while Android Development we can choose Kotlin.

 

Author:
Bhagyashri Sangam | SevenMentor Pvt. Ltd.

Call the Trainer and Book your free demo class for Android Training now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*