MOBILE APPLICATION
DEVELOPMENT
UNIT 2
Anatomy of an Android Application
An Android application consists of the following parts:
• Activities: Represents a single screen in the app.
• Fragments: Subsections of activities that allow for more modular
design.
• Services: Used for long-running operations in the background.
• Broadcast Receivers: Components that respond to system-wide
broadcast announcements.
• Content Providers: Manages shared data between apps
Additionally…
• Resource Files: Images, layouts (XML files), strings, colors, and
dimensions used to define the visual and functional aspects.
• AndroidManifest.xml: Declares app components, permissions, and
other critical app configurations.
• Gradle Files: Automate the building process, handling dependencies
and app configurations.
Android Terminologies
• Activity: Represents a user interface screen in an app.
• Intent: A messaging object to request an action from another app
component.
• Fragment: A modular part of an activity, enabling UI reusability.
• View: Basic building block of the user interface.
• Layout: Arranges views in a specific format (e.g., LinearLayout,
ConstraintLayout).
Application Context
•The Context is a handle to the environment in which an app is running.
•Application Context:
•A global context that persists throughout the app's lifecycle.
•Useful for accessing app-wide resources like themes, databases, or preferences.
Example
Context context = getApplicationContext();
Activities
•An Activity is a single, focused thing the user can do.
•Lifecycle of an Activity:
•onCreate(): Initializes the activity.
•onStart(): Activity becomes visible.
•onResume(): Activity is in the foreground and interacts with the user.
•onPause(): Another activity is partially visible.
•onStop(): Activity is no longer visible.
•onDestroy(): Activity is being destroyed.
Example:
Login Screen → Home Screen → Settings Page (each is an activity).
Services
•Services are used for background tasks without a user interface.
Types of Services:
•Started Service: Runs until it stops (e.g., playing music).
•Bound Service: Tied to a component, running only as long as it's bound (e.g., fetching data for a UI componen
•Foreground Service: Continues running with a notification (e.g., file download).
Example Code for Service:
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Task to perform
return START_STICKY;
}
}
Intents
•Intents allow communication between different app components.
Explicit Intent:
•Targets a specific component.
Example: Navigating from one activity to another.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
• Implicit Intent:Lets Android decide which app can handle the action.
Example: Sharing text via another app
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello!");
intent.setType("text/plain");
startActivity(intent);
Receiving and Broadcasting Intents
Broadcast Receivers: Listen for system-wide broadcasts (e.g., battery low).
Example of a receiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Handle the broadcast
}
}
•Broadcasting Intents: Apps can also send broadcasts to share information.
Android Manifest File and Its Common Settings
The AndroidManifest.xml file:
•Declares activities, services, receivers, and providers.
•Specifies permissions the app needs.
(
•Defines the app's entry point <intent-filter> with MAIN and LAUNCHER actions ).
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Using Intent Filter
• Used in the manifest to define what intents an activity, service, or receiver
can handle.
Example:
<activity android:name=".WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.example.com" />
</intent-filter>
</activity>
Permissions
• Apps must request permissions for accessing sensitive data or
hardware.
• Normal Permissions: Automatically granted (e.g., Internet access).
• Dangerous Permissions: Require user approval (e.g., location,
camera).
Example of Permission in Manifest:
<uses-permission android:name="android.permission.CAMERA" />
Requesting Permission at Runtime:
Example code:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, 1);
}