App components are fundamental building blocks of an Android app, each providing an entry point for the system or user. These components, loosely coupled by the manifest file (AndroidManifest.xml), include:
- Activities
- Services
- Broadcast Receivers
- Content Providers
- Entry point for user interaction, representing a single screen with a UI (user interface).
- Independent instances work together to create a cohesive user experience.
- Key Interactions:
- Tracking the user's on-screen focus.
- Handling process interruptions.
- Facilitating user flows between apps (e.g., sharing).
public class MainActivity extends Activity {
}- Background component for long-running operations, running independently of UI.
- Can play music, fetch data, etc., while allowing other components to interact.
- Two Types: Started services (for background tasks) and Bound services (provides API for other processes).
public class MyService extends Service {
}- Responds to broadcast messages from other apps or the system.
- Handle Communications between Android OS and Applications.
- Handles system-wide events, even when the app is not running.
- Example: Handling alarms to post notifications.
public class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {}
}Definition: Shared Preferences provide a way to store small pieces of data as key-value pairs. They are often used for storing app settings, user preferences, and simple data that needs to persist between app sessions.
Usage:
- Lightweight data storage.
- Ideal for settings and preferences.
- Easily accessed across app sessions.
Implementation:
// Writing to Shared Preferences
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", "JohnDoe");
editor.apply();
// Reading from Shared Preferences
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String username = preferences.getString("username", "DefaultUsername");Definition: File-based storage is a versatile method for storing larger data sets, such as images, documents, or databases. It allows direct control over the file structure and is often used for offline data storage.
Usage:
- Storing large files and data sets.
- Offline data storage.
- Direct control over file structure.
Implementation:
// Writing to File
String data = "Hello, World!";
File file = new File(context.getFilesDir(), "myfile.txt");
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// Reading from File
File file = new File(context.getFilesDir(), "myfile.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// Process each line
}
} catch (IOException e) {
e.printStackTrace();
}- Manages shared app data accessible by other apps (e.g., contacts).
- Provides a standardized method for data sharing.
- Implemented as a subclass of
ContentProvider.
public class MyContentProvider extends ContentProvider {
public void onCreate() {}
}- Fragments:
- Represents a portion of the user interface within an Activity.
- Views:
- UI elements like buttons, lists, and forms.
- Layouts:
- View hierarchies controlling screen format and appearance.
- Intents:
- Messages facilitating communication between components.
- Resources:
- External elements (strings, constants, drawable pictures).
- Manifest:
- Configuration file for the application.
Understanding and utilizing these components is crucial for effective Android app development.