Mobile Application Development-Ppt2
Mobile Application Development-Ppt2
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
•Application Context:
•A global context that persists throughout the app's lifecycle.
•Useful for accessing app-wide resources like themes, databases, or preferences.
Example
•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:
Explicit Intent:
Example of a receiver
(
•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);
}