Chapter 5 - Activity and Multimedia With Database
Chapter 5 - Activity and Multimedia With Database
startActivity(intent);
Types of Intents:
Explicit Intent:
Explicit Intents are used to connect the application internally.
In Explicit we use the name of component which will be affected by Intent.
For Example: If we know class name then we can navigate the app from One
Activity to another activity using Intent.
In the similar way we can start a service to download a file in background process.
Explicit Intents
startActivity(intent);
Here SecondActivity is the JAVA class name where the activity will now be
navigated.
Implicit Intent
In Implicit Intents we do need to specify the name of the component. We just specify the
Action which has to be performed and further this action is handled by the component of
another application.
The basic example of implicit Intent is to open any web page
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“https://www.mountreachsolutions.com”));
startActivity(intent)
Unlike Explicit Intent you do not use any class name to pass through Intent().
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16sp"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnexplitcit"
android:text="Explicit Example"
android:textSize="32sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnimplitcit"
android:text="Implicit Example"
android:textSize="32sp"
android:layout_marginTop="40sp"/>
</LinearLayout>
Step 2: Implement onClick event for Implicit And Explicit Button inside MainActivity.java
package com.mountreachsolution.madbatch2025;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
btnExplicit = findViewById(R.id.btnexplitcit);
btnImplicit = findViewById(R.id.btnimplitcit);
btnExplicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
btnImplicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Step 3: activity_second.xml
package com.mountreachsolution.madbatch2025;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
Output:
Now run the above program in your Emulator. The App will look like this:
First Click on Explicit Intent Example. The SecondActivity will be open within the App:
INTENT FILTER
Intent filters specify the type of intents that an Activity, service or Broadcast
receiver can respond to.
Intent Filter are the components which decide the behavior of an intent.
An Intent Filter declare the capabilities of its parent components i.e Activity,
Service or broadcast receiver.
The code of Intent Filter is used inside AndroidManifest.xml file for an activity.
label: The label / title that appears at top in Toolbar of that particular Activity.
Action:
It represent an activities action, what an activity is going to do.
An Intent Filter element must contain one or more action element.
It is declared with the name attribute and prefix android.intent.action as give
below
Exa mp le : -
<intent- filter>
<action android:name=”android.intent.action.MAIN”/>
</intent-filter>
Android.intent.action.MAIN means that this activity is the entry point of the
application, When you launch the application this activity is created.
Data
There are two forms in which you can pass the data, using URI(Uniform Resource
Identifiers) or MIME type of data.
The <data> element can have several attributes to filter specific data:
1. android:scheme – Specifies the URI scheme (e.g., http, https, tel, mailto, content, file).
2. android:host – Defines the host part of the URI (e.g., www.example.com).
3. android:port – Specifies the port number.
4. android:path – Defines the exact path in the URI.
5. android:pathPattern – A pattern for matching paths (e.g., /user/*).
6. android:pathPrefix – Matches the beginning of the path (e.g., /user matches /user/123).
7. android:mimeType – Defines the type of data the component can handle (e.g., image/*,
text/plain).
Category:
There is a string which contains some additional information about the intent
which will be handled by a component.
ACTIVITY LIFECYCLE
Activity is a screen that user interact with.
Every Activity in android has lifecycle like created, started, resumed, paused, stopped or
restarted,destroyed.
BROADCAST RECEIVERS
The apps can also initiate broadcasts to let other apps know that required data
available in a device to use it.
In android, Broadcast Receiver is implemented as a subclass of
BroadcastReceiver and each broadcast is delivered as an Intent object.
There are following two important steps to make BroadcastReceiver for the
system broadcasted Intent
1. Creating Broadcast Receiver
2. Registering Broadcast Receiver
Creating Broadcast Receiver
A broadcast receiver is implemented as a subclass of BroadcastReceiver and
override the onReceive Method where each message is received as Intent object
Parameter
This Method is triggered when the receiver receive a broadcast.
You also have an Intent object and Context passed as parameter in the onReceive
callback
public class MyReceiver extends BroadcastReceiver
{
@override
Public void onReceiver(Context context, Intent intent)
{
Toast.makeText(context,”Intent Detect”,Toast.LENGTH_LONG).show();
}
}
Broadcast Receiver
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MADBatch2025">
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}
@Override
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 17
protected void onStop() {
super.onStop();
unregisterReceiver(airplaneModeChangeReceiver);
}
}
AirplaneModeChangeReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
if (isAirplaneModeOn(context.getApplicationContext())) {
Toast.makeText(context, "AirPlane mode is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "AirPlane mode is off", Toast.LENGTH_SHORT).show();
}
}
FRAGMENT
It is the modular section of the Android activity that is very helpful in creating UI
designs that are flexible in nature and auto-adjustable based on the device screen size.
<fragment> tag is used to insert the fragment in an android activity layout. By dividing
the activity’s layout multiple fragments can be added in it.
Example:-
activity_main:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/fragment1"
android:name="com.mountreach.madbatch2025.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment2"
android:name="com.mountreach.madbatch2025.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 21
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java
package com.mountreach.madbatch2025;
//package packnamename;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment1.java
package com.mountreach.madbatch2025;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@Override
public View onCreateView(LayoutInflater
inflater, ViewGroup container,
Bundle
savedInstanceState) {
// Inflate the layout for this fragment
return
inflater.inflate(R.layout.fragment_1, container,
false);
}
}
Fragment2.java
package com.mountreach.madbatch2025;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Output:-
Feature of Service:
Service is an Android Component without an UI
Services run indefinitly unless they are explicitly stopped or destroyed
It can be started by any other application component.
Components can even infact bind to a service to perform Interprocess- Comminication
It stops itself by calling stopself() or is stopped by a Android component by calling
stopService().
The Android platform provides and runs predefined system services and every
Android application can use them, given the right permissions.
These system services are usually exposed via a specific Manager class.
Example:
Vibrator Service-
Location Service
WiFi Service
Audio Service
Notification Service
There can be two forms of a service.The lifecycle of service can follow two
different paths: started or bound.
Started
Bound
1) Started Service
A service is started when component (like activity) calls startService() method,
now it runs in the background indefinitely.
It is stopped by stopService() method.
The service can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls bindService() method
The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.
onCreate()
This is the first callback which will be invoked when any component starts the service.
If the same service is called again while it is still running this method wont be invoked.
onStartCommand()
This callback is invoked when service is started by any component by calling startService().
onBind()
This is invoked when any component starts the service by calling onBind.
onUnbind()
This is invoked when all the clients are disconnected from the service.
This is invoked when new clients are connected to the service. It is called after onUnbind
onDestroy()
This is a final clean up call from the system. This is invoked just before the service is being
destroyed.
activity_main.xml
<Button
android:id="@+id/buttonStart”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" />
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service" />
<Button
android:id="@+id/buttonNext "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 28
android:text="Next Page" />
</LinearLayout>
activity_next.xml
It contains only one textview displaying the message Next Page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Service class
Now create the service implemenation class by inheriting the Service class and
overridding its callback methods.
MyService.java
Package example.javatpoint.com.androidservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
MediaPlayer myPlayer;
@Nullable
@Override
public IBinder onBind(Intent
intent) { return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
Activity class
Now create the MainActivity class to perform event handling. Here, we are
writing the code to start and stop service. Additionally, calling the second
activity on buttonNext.
MainActivity.java
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 31
package example.javatpoint.com.androidservice;
import android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = findViewById(R.id.buttonStart);
buttonStop = findViewById(R.id.buttonStop);
buttonNext = findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(th is);
}
public void onClick(View src)
{
switch (src.getId())
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 32
{
case R.id.buttonStart:
break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class)); break;
case R.id.buttonNext:
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}
NextPage class
Now, create another activity.
File: NextPage.java
android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
Declare the Service in the AndroidManifest.xml file Finally, declare the service in
the manifest file.
File: AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launc
her"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
android:name=".MyService "
android:enabled="true" />
</application>
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 34
</manifest
> Output:
Java Class call the Native C library Libmedia throught Media JNI(Java Native Interface)
Libmedia library communicates with Media Server guard process through Android’s
Binder IPC (inter process communication) mechanism.
Media Server Process creates the corresponding multimedia service according to the java
Multiplemedia application
The whole communication between Libmedia and Media Server forms a Client/Server
model.
In Media Server guard process, it calls OpenCore multimedia engine to realize the
specific multimedia processing functions. And the OpenCore engine refers to the
PVPlayer and PVAuthor.
Once you have created the MediaPlayer object you can call some methods to start or stop
the music
o mediaPlayer.start()
o mediaPlayer.stop()
Activity class
File: MainActivity.java
package com.example.audiomediaplayer1;
importandroid.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
importandroid.widget.MediaController;
import android.widget.VideoView;
{ @Override
}catch(Exception e){e.printStackTrace();}
}
}
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 39
Example :-
main_activity.xml
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.IOException;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button1);
pause = (Button) findViewById(R.id.button2);
stop = (Button) findViewById(R.id.button3);
videoView = (VideoView) findViewById(R.id.videoView1);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//creating media player
mp = MediaPlayer.create(MainActivity.this, R.raw.meethi_boliyaan);
Toast.makeText(MainActivity.this, "Music Start", Toast.LENGTH_SHORT).show();
if (!mp.isPlaying()) {
mp.start();
String videoPath = "android.resource://" + getPackageName() +
"/raw/meethi_boliyaan_video";
videoView.setVideoPath(videoPath);
videoView.start();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Music Pause", Toast.LENGTH_SHORT).show();
if (mp.isPlaying()) {
mp.pause();
videoView.pause();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Music Stop", Toast.LENGTH_SHORT).show();
if (mp.isPlaying()) {
mp.stop();
videoView.stopPlayback();
}
});
}
}
Create raw Directory and Paste your mp3 and mp4 sond inside this
Output:-
TEXT TO SPEECH
In android, by using TextToSpeech class we can easily convert our text into voice
and it supports different types of speaking languages.
We can choose the speaking language based on our requirements in the android
application.
The android TextToSpeech instance can only be used to synthesize text once it
has completed its initialization.
In order to use this class, you need to instantiate an object of this class
During the initialization, we can set the audio pitch rate, audio speed, type of
language to speak, etc. based on our requirements.
Apart from the speak method, there are some other methods available in the
TextToSpeech class. They are listed below −
1 speak()
2 getLanguage()
3 isSpeaking()
4 setPitch(float pitch)
This method sets the speech pitch for the TextToSpeech engine.
5 setSpeechRate(float speechRate)
6 stop()
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text to Speech"
android:textSize="36sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Text"
android:layout_marginBottom="20dp"
android:hint="Enter your text"
android:gravity="center"
android:textSize="16dp"
android:layout_marginTop="16sp"/>
<Button
android:layout_width="wrap_content"
android:id="@+id/btnText"
android:layout_height="wrap_content"
android:text="Click"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java
package com.mountreach.madbatch2025;
//package packnamename;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
EditText Text;
Button btnText;
TextToSpeech textToSpeech;
Text = findViewById(R.id.Text);
btnText = findViewById(R.id.btnText);
textToSpeech.speak(Text.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
}
});
}
}
Output:-
ANDROID SENSOR
List different types of sensors used in android. S-23 2 Marks
Write a program to display the list of sensors supported by device. S-24 6 Marks
Most of the android devices have built-in sensors that measure motion,
orientation, and various environmental condition.
Types of Sensors
Android supports three types of sensors:
o Motion Sensors
These are used to measure acceleration forces and rotational forces along with
three axes.
Motion sensors are useful for monitoring device movement such as
tilt,shake,rotation or swing
o Position Sensors
These are used to measure the physical position of device.
These sensors are use for applications related to navigation, augmented reality (AR),
and location-based services
o Environmental Sensors
These are used to measure the environmental changes such as temperature, humidity etc.
Example:-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of Sensors in Your Device"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="10dp" />
<ListView
android:id="@+id/sensorListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.xml
package com.mountreach.madbatch2025;
//package packnamename;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Output:-
ASYNCTASK
and keep the UI thread light thus making the application more responsive.
Due to this single thread model tasks that take longer time to fetch the response
can make the application non- responsive.
The three generic types used in an android AsyncTask class are given below: -
Params : The type of the parameters sent to the task upon execution
Progress : The type of the progress units published during the background computation
Result : The type of the result of the background computation
CAMERA IN ANDROID
Q) Write a program to capture an image using camera and display it. 6 Marks s-22
In Android, Camera is a hardware device that allows capturing pictures and videos
in your applications.
Example
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CAMERA"
android:textSize="20dp"
android:gravity="center"
android:layout_marginTop="20sp"
/>
<ImageView
android:id="@+id/image"
android:layout_width="250dp"
android:layout_height="250sp"
android:scaleType="fitXY"
android:src="@drawable/captureimage"
android:layout_gravity="center"/>
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAKE PHOTO"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.class
package com.mountreach.madbatch2025;
//package packnamename;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAMERA_REQUEST)
{
Bitmap image= (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);
}
}
}
Output:-
BluetoothAdapter class
Android Provides BluetoothAdapter class to communicate with Bluetooth.
By the help of BluetoothAdapter class, we can perform fundamental tasks such as
initiate device discovery, query a list of paired (bonded) devices, create a
BluetoothServerSocket instance to listen for connection requests etc.
Its Syntax is give below
Private BluetoothAdapter BA
BA = BluetoothAdapter.getDefaultAdapter();
AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List all Paired devices"
android:onClick="list"
android:id="@+id/button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView1"
android:textColor="#ff34ff06"
android:textSize="25dp"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
/>
</LinearLayout>
MainActivity.class
package com.mountreach.madbatch2025;
//package packnamename;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Set;
Button b1;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}
lv.setAdapter(adapter);
}
}
Output:-
SQLite
Q) Describe with example, how to create a simple database in SQLite (Assume
suitable data) s- 22 4 marks
Q) Develop an android application for taking student feedback with database
connectivity. S-23 6 Marks
Q) Develop an application to store customer's details like, customer-id, customer-name,
mobile number, address, pin-code and retrieve customer information using customer-id
in SQLite databases.
(Any other relevant logic can be considered) w-22 6 Marks
Q) Write significance of SQLite database in android. S-24 4 Marks
SQLiteDatabase class
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 58
It Containt method to be performed on SQLite Database such as create, update,
delete,select etc.
To use these method, we need to instantiaste SQLiteDatabase as follow
SQLiteDatabase db;
Note:- Before you can use this,you must import the android.database.sqlite.SQLiteDatabase
namespace in your application
Example:-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:text="Create SQLite Database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:id="@+id/button" />
</LinearLayout>
MainActivity.java
package com.mountreach.madbatch2025;
//package packnamename;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
SQLiteDatabase sqLiteDatabaseObj;
Button btnCreatedatabse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreatedatabse = (Button)findViewById(R.id.button);
btnCreatedatabse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase",
Context.MODE_PRIVATE, null);
} });
}
}
Output:-
Example 2:-
Develop an application to store customer's details like, customer-id, customer-name,
mobile number, address, pin-code and retrieve customer information using customer-
id in SQLite databases. (Any other relevant logic can be considered)
<TextView
android:text="Insert Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView"
android:gravity="center"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:id="@+id/editid"
android:layout_below="@+id/textView"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editname"
android:layout_below="@+id/editid"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Mobile No."
android:id="@+id/editmobile"
android:layout_below="@+id/editname"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:lines="3"
android:id="@+id/editaddress"
android:layout_below="@+id/editmobile"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Pin Code"
android:id="@+id/editpincode"
android:layout_below="@+id/editaddress"/>
<Button
android:text="Insert Data"
MR. NIKHIL SHENDE |
Chapter 5 Activity and Multimedia with Databases by Nikhil Shende – Mountreach
Solution Pvt Ltd 61
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editpincode"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
<TextView
android:text="Search Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:id="@+id/textView1"
android:gravity="center"
android:textSize="20dp"
android:layout_below="@+id/button"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:id="@+id/editsearchid"
android:layout_below="@+id/textView1"/>
<Button
android:text="Search Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editsearchid"
android:layout_centerHorizontal="true"
android:id="@+id/button1" />
</LinearLayout>
MainActivity:-
package com.mountreach.madbatch2025;
//package packnamename;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
SQLiteDatabase sqLiteDatabaseObj;
EditText editTextID, editTextName, editMobileNo, editAddress, editPincode,
editSearchid;
String cid, cname, cmobile, caddress, cpincode, sql_query, sid;
Button EnterData, SearchData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Output:-