[go: up one dir, main page]

0% found this document useful (0 votes)
30 views21 pages

MP Lecture4 Presentation

The document outlines key components of mobile programming in Android, including Services, Broadcast Receivers, Content Providers, and Fragments. It explains the functionality and implementation of these components, as well as practical applications such as creating an audio service and using UI elements like ProgressDialog. Additionally, it covers layouts, resources, and the AndroidManifest.xml file, emphasizing their roles in app development.

Uploaded by

albsrawys686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views21 pages

MP Lecture4 Presentation

The document outlines key components of mobile programming in Android, including Services, Broadcast Receivers, Content Providers, and Fragments. It explains the functionality and implementation of these components, as well as practical applications such as creating an audio service and using UI elements like ProgressDialog. Additionally, it covers layouts, resources, and the AndroidManifest.xml file, emphasizing their roles in app development.

Uploaded by

albsrawys686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

2024-2025 Lecture 4

MOBILE PROGRAMMING
ASST. LECT. GHASAQ B. ABDULHUSSEIN
ghasaq@uobabylon.edu.iq
3RD Stage
OUTLINE

 Core Building Blocks (App Components) (Cont…):


 Service
 Broadcast Receivers
 Content Provider
 Fragment
 Layout & Manifest & View & Resources
 Practical:
 Make an audio Service
 ImageView & ImageButton
 Progress dialog
APP COMPONENTS (CORE BUILDING BLOCKS) : SERVICE
 In addition to Activities, Android apps can also include Services, which are background processes that perform long-
running tasks. Services can be started or stopped by other components, such as Activities, and can run in the background
even when the user is not interacting with the app. Services can be used to perform tasks such as playing music, updating
data, or connecting to a remote server.
 Service is a task that runs in the background without the user’s direct interaction. In fact, it does the majority of
processing for an application. Developers can sub-class the Service class to write their own custom service.
 Activities are short-lived and can be shut down at any time, such as when the user presses the BACK button. Services,
on the other hand, are designed to keep running, if needed, independent of any activity, for a moderate period of time.
You might use a service for checking for updates to an RSS feed, or to play back music even if the controlling activity is
no longer operating.You will also use services for scheduled tasks and for exposing custom APIs to other applications on
the device, though the latter is a relatively advanced capability.
 A service is a component that runs in the background to perform long-running operations or to perform work for
remote processes.
 A service does not provide a user interface.
 A service is implemented as a subclass of Service class. 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.
 Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.
APP COMPONENTS (CORE BUILDING BLOCKS) : SERVICE (CONT…)

 Once started, a service might continue running for some time, even after the user
switches to another application. Additionally, a component can bind to a service to
interact with it and even perform interprocess communication (IPC). For example, a
service can handle network transactions, play music, perform file I/O, or interact with
a content provider, all from the background.
 Basically, there are two types of services in Android, which are:
1. Bound Services: It’s bound to other components and runs only till the component
to which it is bounded runs.
2. Unbound Services: It’s a type of service which is not bounded to any components.
Once started, it will run in the background even after the component that started
the service gets killed.
APP COMPONENTS (CORE BUILDING BLOCKS): BROADCAST
RECEIVERS

 Broadcast Receivers are another type of component that can be used in Android apps. They allow apps to receive and react to system
announcements or deliver broadcasts to other apps or components. Broadcast Receivers can be used to perform tasks such as
responding to system-wide broadcasts, such as the BOOT_COMPLETED broadcast, which is sent when the phone completes its booting
process.
 The system, or applications, will send out broadcasts from time to time, for everything from the battery getting low, to when the screen
turns off, to when connectivity changes from WiFi to mobile data. A broadcast receiver can arrange to listen for these broadcasts and
respond accordingly.
 This component responds to system-wide broadcast announcements. It also doesn't provide any user interface.
 Broadcast Receivers is different from service component in anyways as it works on events based broadcasts and does minimum work.
For example of broadcasts initiated by the system are:
 1. Warning that the battery is getting low
 2. Screen turned off
 3. Change of time zone
 4. The camera has been used to take a picture
 While programming, we can use Broadcast receivers to receive these broadcasted messages and behave accordingly.
 A broadcast receiver is implemented as a subclass of BroadcastReceiverclass and each message is broadcaster as an Intent object.
APP COMPONENTS (CORE BUILDING BLOCKS) : CONTENT PROVIDER
 Content Provider is a set of data wrapped up in a custom Application Programming Interface (API) to read and
write it.
 Content Providers are a type of component that allows apps to share data between apps or app components.
They expose an entry point to other apps, through which they can fetch, modify, or delete the app's shared data.
Content Providers can be used to perform tasks such as retrieving data from a database or file system, and
publishing data to other apps.
 Content providers provide a level of abstraction for any data stored on the device that is accessible by multiple
applications. The Android development model encourages you to make your own data available to other
applications, as well as your own — building a content provider lets you do that, while maintaining a degree of
control over how your data gets accessed.
 The content provider provides a uniform singular interface to the content and data and provides a consistent
interface to retrieve/store data via RESTful model supporting create, read, update, and delete (CRUD) operations.
 You can store the data in the file system, a SQLite database, on the web, or any other persistent storage location
your app can access. Through the content provider, other apps can query or even modify the data (if the content
provider allows it).
APP COMPONENTS (CORE BUILDING BLOCKS) : CONTENT
PROVIDER (CONT…)

 So, for example,


 Ex1: if you have a PDF file that you downloaded on behalf of the user, and you want to allow the
user to view that PDF file, you might create a content provider to make that PDF file available to
other apps.You can then start up an activity that will be able to view that PDF, where Android and
the user will determine what PDF-viewing activity handles that request.
 Ex2, the Android system provides a content provider that manages the user’s contact information.
As such, any app with the proper permissions can query part of the content provider (such as
ContactsContract.Data) to read and write information about a particular person.
 Content providers are also useful for reading and writing data that is private to your app and
not shared. For example, the Note Pad sample app uses a content provider to save notes.
 You must have proper permissions to access data from other applications.
 A content provider is implemented as a subclass of ContentProvider class.
LAYOUTS
 Layouts : A visual structure for any user interface. For example, user interface for activity, widgets, fragments
etc. There are several types of layouts in Android Studio, including static, dynamic, and hybrid layouts.
 Static layouts are defined in XML files and are typically used for simple layouts that do not require any
changes at runtime. These layouts are rendered by the Android framework and are not dynamic, meaning
that they cannot be changed once the app is launched. Examples of static layouts include RelativeLayout,
LinearLayout, and GridView.
 Dynamic layouts, on the other hand, are created at runtime using the Android API. These layouts are
typically used for more complex layouts that require changes to be made at runtime, such as when a user
interacts with the app. Dynamic layouts can be created using the addView() method, which adds a new
view to the layout, or using the inflate() method, which inflates a layout from an XML file.
 Hybrid layouts are a combination of static and dynamic layouts. They define some default components in
static layout files and can be reused dynamically by invoking LayoutInflater.inflate. Hybrid layouts can also
manipulate the attributes of the defined components, allowing for more flexibility than static layouts.
 You can declare layouts in two ways.
1. Statically:You can declare layouts in an XML file. xml file is added in res => layout folder of any Android
application. Examples of static layouts include RelativeLayout, LinearLayout, and GridView.
2. Dynamically:You can declare layouts at runtime as well. To create layouts at runtime, you would need to
add related code in java file.
APP COMPONENT (CONT…)

 Resources : There are many more things you need while creating user interface for an
activity in Android application (images, icons, animations, audio, video etc). They are stored in
“res folder” in any Android application. Resources are static bits of information held outside
the Java source code.
 Views: This is a basic building block for user interface component. It is a rectangular area on
the screen. We can draw or handle some events in this rectangular area. Also, View is base
class for widgets (used to create user interface components).
 Fragment: Is a part of a user interface in an activity. Basically, Can be using one or more
fragments in an activity. While activity is running, can be assuming fragment as a modular part
of the activity that has its own life cycle; can receive its own input; can be added or removed
dynamically. Fragments are an optional layer you can put between your activities and your
widgets. Fragments are not activities, though they can be used by activities. You define a
fragment, much like you might define an activity, with layouts and lifecycle methods and so on.
However, you can then host that fragment in one or several activities, as needed.
Functionally, fragments are Java classes, extending from a base Fragment class.
 Manifest : Every Android application must have AndroidManifest.xml file in its root directory. It contains a short description
of the Android application. For example, It contains the package name of the application (that acts as a unique identifier of
the application), information about the components of the application (Activities, services, broadcast receivers, content
providers), permissions needed to run the application, minimum level of API that application needs to run etc.
 AndroidManifest.xml is an XML file describing the application being built and what components — activities, services, etc. —
are being supplied by that application.You can think of it as being the “table of contents” of what your application is about.
 By default, when you create a new Android project, you get a single element inside the <activity> element. The <activity>
element supplies android:name for the class implementing the activity, android:label for the display name of the activity, and
(sometimes) an <intent-filter> child element describing under what conditions this activity will be displayed. The stock
<activity> element sets up your activity to appear in the launcher, so users can choose to run it.
 you will find other elements being added to the manifest, such as:
 <uses-permission>, to tell the user that you need permission to use certain device capabilities, such as accessing the
Internet
 <uses-feature>, to tell Android that you need the device to have certain features (e.g., a camera), and therefore your app
should not be installed on devices lacking such features
 <meta-data>, for bits of information needed by particular extensions to Android, such as by FileProvider.
 What Does Android Have To Do with Gradle? Google has published the Android Gradle Plugin,
which gives Gradle the ability to build Android projects. Google is also using Gradle and the Android
Gradle Plugin as the build system behind Android Studio.
 What Are Widgets? In computer programming, a widget (or control) is an element of a graphical
user interface (GUI) that displays an information arrangement changeable by the user, such as a
window or a text box. The defining characteristic of a widget is to provide a single interaction point
for the direct manipulation of a given kind of data. In other words, widgets are basic visual building
blocks which, combined in an application, hold all the data processed by the application and the
available interactions on this data.
 Note : If you initialize something in onCreate(), clean it up in onDestroy(). If you initialize something in
onStart(), clean it up in onStop(). If you initialize something in onResume(), clean it up in onPause(). In
other words, stick to the pairs. For example, do not initialize something in onStart() and try to clean it
up in onPause(), as there are scenarios where onPause() may be called multiple times in
succession.Which pairs of lifecycle methods you choose is up to you, depending upon your needs. You
may decide that you need two pairs (e.g., onCreate()/onDestroy() and onResume()/onPause()).
PRACTICAL :
 Making an audio service app

1. assigning ID of startButton to the object start


2. assigning ID of stopButton to the object stop
3. declaring listeners for the buttons to make them
respond correctly according to the process
 Creating a service
 declaring object of MediaPlayer
 execution of service will start on calling this method (onStartCommand)
 execution of the service will stop on calling this method ( onDestroy )
 After creating a service in your app, the manifest file will automatically add the reference of the service to its file.
 Using image and image button
 Set image to the image view while coding:
PROGRESS DIALOG

 One common UI feature in an Android device is the “Please wait”


dialog that you typically see when an application is performing a
long-running task.
 For example, the application might be logging in to a server before
the user is allowed to use it, or it might be doing a calculation
before displaying the result to the user. In such cases, it is helpful
to display a dialog, known as a progress dialog, so that the user is
kept in the loop.
 Android provides a ProgressDialog class you can call when you
want to display a running meter to the user. ProgressDialog is easy
to call from an activity.
PROGRESS DIALOG (CONT…)
 To create a progress dialog, you create an instance of
the ProgressDialog class and call its show() method:
progressDialog = ProgressDialog.show(this,"Please Wait",
"Processing...",true);
 This displays the progress dialog. Because this is a
modal dialog, it will block the UI until it is dismissed.
 To close the dialog, you create a timer that calls the
dismiss() method after three seconds.
CountDownTimer timer = new
CountDownTimer(3000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
progressDialog.dismiss();
}
}.start();
PRACTICAL (CONT…)

 Put 2 Buttons to play and stop an audio after viewing a Progress window for 3
seconds (Loading...).
WE HAVE LEARNED TODAY:

Core Building Blocks (App Components) Service

Broadcast Receivers

Content Provider

Fragment

Layout & Manifest & View & Resources

Practical: Make Service , ImageView & ImageButton &Progress dialog


THANK YOU

You might also like