Part A: 2020 March
Part A: 2020 March
Module 3 : Android
2020 March
2021 April
6. Define Service.
A Service is a component in Android that runs in the background to perform long-running
operations or to handle network transactions. It does not have a user interface and can run
even when the app is not visible on the screen. Services can be started and stopped
programmatically and can communicate with other components using Intents or Binders.
7. Define Multimedia.
Multimedia in Android refers to the use of audio, video, and images in Android applications.
Android provides APIs for playing and recording audio and video, displaying images and
video frames, and capturing images and videos using the device's camera. Multimedia can
be used in various types of applications, such as games, social media, and entertainment
apps.
2022 April
2020 March
● Application layer: This layer is responsible for the user interface of the multimedia
application. It provides the views and controls that the user uses to interact with the
multimedia content.
● Media layer: This layer is responsible for the playback and recording of multimedia
content. It provides the codecs and other components that are necessary to decode
and encode multimedia content.
● Hardware layer: This layer is responsible for the interaction with the hardware. It
provides the drivers and other components that are necessary to access the
multimedia hardware.
The multimedia framework architecture is designed to be modular and flexible. This allows
developers to add new features and functionality to the framework without having to modify
the existing layers.
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US);
initialized = true;
}
}
This will speak the text "Hello, world!" using the device's text-to-speech engine.
2021 April
ii) onPause() is called when an activity is no longer in the foreground and has lost focus. It
is often used to save the state of the activity, such as user input, so that it can be restored
later when the activity comes back into focus.
iii) onDestroy() is called when an activity is being destroyed and removed from memory. It
is used to clean up any resources that were allocated by the activity, such as unregistering
listeners or releasing memory.
iv) onStop() is called when an activity is no longer visible to the user. It is often used to
release resources that are no longer needed while the activity is in the background, such as
stopping a media player or releasing a camera.
Java Code:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStop() {
super.onStop();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Audio"
android:onClick="playAudio"/>
</LinearLayout>
To play an audio file in Android, we define a MediaPlayer object and initialize it using the
create() method. The start() method is used to play the audio file on the click of a
button. The onStop() method is used to release the MediaPlayer object and free up
system resources. The audio file must be added to the "raw" folder in the "res" folder of the
Android project.
2022 April
Intents can be created using two constructors: explicit and implicit. Explicit intents are used
to start a specific component in the same application, while implicit intents are used to start
any component that can perform a particular action regardless of the application.
To create an intent, you first need to specify its action, which is a string that identifies the
action to be performed. Then, you can add any additional data or extras to the intent, such
as a URI or a bundle of key-value pairs. Finally, you can use the startActivity() method to
start the activity associated with the intent.
18. What are the classes used to play Audio and Video in
Android?
The classes used to play audio are MediaPlayer and AudioManager. MediaPlayer is a class
that can play various types of audio formats and provides various methods to control the
playback, such as start(), pause(), stop(), and release(). AudioManager is used to control the
volume and audio settings of the device.
2020 March
Java Code:
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
speakButton = findViewById(R.id.speak_button);
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,
null);
}
});
}
@Override
public void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result ==
TextToSpeech.LANG_NOT_SUPPORTED) {
speakButton.setEnabled(false);
} else {
speakButton.setEnabled(true);
}
}
}
}
XML Code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text to speak" />
<Button
android:id="@+id/speak_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Speak" />
</RelativeLayout>
2021 April
An activity provides a window for drawing the user interface and handles user interactions
such as button clicks, touch events, and keystrokes. It also manages the lifecycle of the app,
handling events such as when the app is started, paused, resumed, and stopped.
When an activity is launched, it is placed on the top of the activity stack, and the user
interacts with it until it is finished or the user navigates to another activity. The Android
system manages the activity stack, and activities can be launched and closed dynamically
based on user interactions or programmatic logic.
Activities are defined in the AndroidManifest.xml file, and each activity has a unique name
and can be launched by an intent. An activity can also pass data to another activity using
intent extras or receive data from another activity using startActivityForResult() method.
Overall, activities are a critical part of Android app development and provide the foundation
for creating interactive user interfaces and managing the lifecycle of an application.
b) Use of Intents
Intents are used to communicate between the components of an Android application, such
as activities, services, and broadcast receivers, as well as to communicate with other
applications on the device. Intents are used to initiate actions, pass data, and start activities.
There are two types of intents: explicit intents and implicit intents. Explicit intents are used to
start a specific component within the same application, whereas implicit intents are used to
start a component outside of the application.
Here are two examples of how to use intents in Android:
In the above example, we pass two pieces of data (name and age) from the sending activity
to the receiving activity using an explicit intent. The receiving activity retrieves the data from
the intent using the getIntent() method and the getStringExtra() and getIntExtra() methods.
2022 April
In summary, the Service life cycle is responsible for the creation, execution, and destruction
of services while the Broadcast life cycle is responsible for handling incoming broadcasts
and providing a response. Understanding these life cycles is crucial for building robust and
efficient Android applications.