Android Components with Examples
Rishabh Kumar
Posted on January 18, 2022
What are Components
App components are the essential building blocks of an Android app. Each component is an entry point through which the system or a user can enter your app. Some components depend on others. All the components are coupled together in a file name AndroidMenifest.xml
which tells how they are going to interact with each other.
Type of Components
- Activities
- Services
- Broadcast Receivers
- Content Provides
Detail Explanations
Activities
Activity is screen what you see and interact with on your phone. For example, when you open WhatsApp you see activity. Now you click on a chat you see another activity. You can create an activity with build-in class Activity
or AppCompatActivity
Code snippet
- Activity
public class MainActivity extends Activity {
}
- AppCompatActivity
public class MainActivity extends AppCompatActivity {
}
Services
A service is a general-purpose entry point for keeping an app running in the background for running multiple tasks in the background. It is a component that runs in the background to perform long-running operations or to perform work for remote processes. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity. You can create your service with built-in class Service
Code snippet
public class CustomService extends Service {
}
Broadcast Receivers
Broadcast Receivers enables the system to deliver events to the app outside of a regular user flow, allowing the app to respond to system-wide broadcast announcements. For example, you can fire broadcast when you receive a message, you receive a call, you turn on airplane mode.
There are two kinds of Broadcast Receivers.
- Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed.
- Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized. #### Code snippet
public class MyReceiver extends BroadcastReceiver {
public void onReceive(context,intent){}
}
Content Providers
A content provider manages a shared set of app data that you can store in the file system, in a SQLite database, on the web, or on any other persistent storage location that your app can access. Through the content provider, other apps can query or modify the data if the content provider allows it.
Code snippet
public class MyContentProvider extends ContentProvider {
public void onCreate(){}
}
Posted on January 18, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.