- Published on
Android Fundamentals: Practical Coding
- Authors
- Name
- Dan Tech
- @dan_0xff
This article provides practical code samples for you to work with Android Fundamentals Components. This article is extremely important for all Android developers, requiring learning, understanding, and mastery to work well in the future.
Install & Create a Project with Android Studio
To download Android Studio, please visit https://developer.android.com/studio and select the Stable Android Studio version for your operating system.
After installation, open Android Studio.
Click the New Project button, select No Activity, enter the project name, and select Finish.
After waiting for the project to load, you will have an interface like this.
Open the build.gradle file in the app module to install ViewBinding and DataBinding.
plugins {
id("kotlin-kapt")
}
buildFeatures {
viewBinding = true
dataBinding = true
}
Why does this course not use Compose and still use ViewBinding and DataBinding?
- Currently, Android interviews in the market still use Android View a lot.
- Android View is the foundation. You can learn Compose or not, but you can't skip Android View.
- Learn how to work with Android View first; Write standardized ViewModel code to easily migrate from View to Compose in the future.
Using Activity in Android
Create MainActivity and the corresponding XML file through the menu in Android Studio. Select MainActivity as the Launcher Activity (Launcher Activity is the Activity that runs first when we open the application from the App Icon).
Create the next 4 Activities representing the 4 types of Launch Modes we will Demo: Standard Activity, Single Top Activity, Single Task Activity, Single Instance Activity.
Note: Set up Launch Mode for these Activities in the AndroidManifest file.
Implement Start Activity logic inside each Activity using ViewBinding.
startActivity(Intent(this@MainActivity, StandardActivity::class.java)) // code to call to initialize an Activity in Android
Using Service in Android
Practicing BackgroundService
Create a BackgroundService class through the Android Studio menu
Place logs sequentially in the functions: onCreate, onDestroy, onStartCommand
Implement the onStartCommand function to simulate a heavy IO task
Implement Start Service logic from MainActivity
Demo calling Start Service multiple times and view logs
Implement Stop Service logic from Main Activity
Background Service Notes
Refer to Google Android Developer
- Background Service will be stopped when the application is killed. Engineers are forced to manually call this Service again in the source code if they want to restart it.
Practicing ForegroundService
Create a ForegroundService class from the Android Studio Menu
Access the AndroidManifest file and set up the necessary permissions to run the Foreground Service.
Implement the necessary logic to run a Foreground Service
Start and Stop Foreground Service from Activity
A few notes on Foreground Service
Refer to Google Android Developer
- Foreground Service will continue to run if we close the application.
- In the special case where the operating system kills the Foreground Service to use memory. The Foreground Service will automatically restart afterwards when there are sufficient resources.
Using Broadcast Receiver in Android
Practicing with Broadcast Receiver
Create a Broadcast Receiver in Android Studio, named SimpleReceiver
Register Intent Filter in AndroidManifest
Demo Receiver when Restarting the machine (For this part, run the app on the machine and restart it)
Demo Local manual Broadcast Receiver
// execute sending Broadcast Receiver in your functions
sendBroadcast(Intent(SimpleReceiver.SIMPLE_ACTION))
Using Content Provider in Android
Use Content Provider to query SMS on the device
Register SMS read permission
Some notes when deciding to use Content Provider
- Although Content Provider is used to write and read data in Android apps. But we don't necessarily have to use Content Provider for data storage tasks. Some other solutions can be listed here: Room Database, Jetpack DataStore, Level Database …
When should you consider using Content Provider?
- When you need to access data from another application: SMS, Photo Gallery, File, …
- When you want to export your application's data so that other applications can use it. For example: Sharing login information, tracking information between apps in Meta's ecosystem;
Above is all the knowledge about Android Fundamentals that all Android Devs need to know. Hope you learn well!