Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views25 pages

Chapter 5

The document covers various components of Android development including Intents for communication between app components, Fragments for modular UI design, and Services for background operations. It also discusses permissions for accessing user data, audio/video playback using MediaPlayer and VideoView, and text-to-speech functionality. Additionally, it provides insights into AsyncTask for background processing, camera usage, Bluetooth API, and SQLite for database management in Android applications.

Uploaded by

koliharsha600
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)
4 views25 pages

Chapter 5

The document covers various components of Android development including Intents for communication between app components, Fragments for modular UI design, and Services for background operations. It also discusses permissions for accessing user data, audio/video playback using MediaPlayer and VideoView, and text-to-speech functionality. Additionally, it provides insights into AsyncTask for background processing, camera usage, Bluetooth API, and SQLite for database management in Android applications.

Uploaded by

koliharsha600
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/ 25

CHAPTER 5

INTENT-

An Intent is a messaging object you can use to request an action from another app component. Intents
are used for facilitating communication between components like Activities, Services and Broadcast
Receivers.

Intent is the message that is passed between components such as activities. Android uses Intent for
communicating between the components of an Application and also from one application to another
application.

Types: • Explicit Intent • Implicit Intent

syntax to create an intent and start another activity. 2 M

Intent i = new Intent(this, ActivityTwo.class); //create intent


startActivity(i); //start activity

FRAGMENTS-

→ In Android, Fragment is a part of an activity which enables more modular activity design.
→ It will not be wrong if we say a fragment is a kind of sub-activity.
→ It represents behaviour or a portion of user interface in an Activity.
→ We can combine multiple Fragments in Single Activity to build a multi panel UI and reuse a
Fragment in multiple Activities.
→ We always need to embed Fragment in an activity and the fragment lifecycle is directly affected
by the host activity’s lifecycle.
→ We can create Fragments by extending Fragment class or by inserting a Fragment into our
Activity layout by declaring the Fragment in the activity’s layout file, as a element.
→ Android Fragment is the part of activity, it is also known as sub-activity. There can be more than
one fragment in an activity.
→ Fragments represent multiple screen inside one activity.
ANDROID SERVICE

→ Android service is a component that is used to perform operations on the background such as
playing music, handle network transactions, interacting content providers etc.It doesn't has
any UI (user interface).
→ The service runs in the background indefinitely even if application is destroyed. Moreover,
service can be bounded by a component to perform interactivity and inter process
communication (IPC).

Features of Service

→ Service is an Android Component without an UI


→ It is used to perform long running operations in background. 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 can still be running even if the application is killed unless it stops itself by calling stopself()
or is stopped by a Android component by calling stopService().
→ If not stopped it goes on running unless is terminated by Android due to resource shortage
→ The android.app.Service is subclass of ContextWrapper class.

Android platform service

→ 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. Access to them can
be gained via the getSystemService() method.

PERMISSION

The purpose of a permission is to protect the privacy of an Android user. Android apps must request
permission to access sensitive user data (such as contacts and SMS), as well as certain system
features (such as camera and internet). Depending on the feature, the system might grant the
permission automatically or might prompt the user to approve the request.

On all versions of Android, to declare that your app needs a permission, put a <uses-permission>
element in your app manifest, as a child of the top-level <manifest> element.

For example, an app that needs to access the internet would have this line in the manifest:

<manifest xmlns:android=http://schemas.android.com/apk/res/android
package="com.example.snazzyapp">

<uses-permission android:name="android.permission.INTERNET"/>

<!-- other permissions go here -->

<application ...>

...

</application>

</manifest>
Write send n receive sms as An example for permissions

PLAY AUDIO AND VIDEO

→ We can play and control the audio files in android by the help of MediaPlayer class.
→ MediaPlayer class The android.media.MediaPlayer class is used to control the audio or video
files. Methods of MediaPlayer class There are many methods of MediaPlayer class.
→ Some of them are as follows:
Android Video Player

By the help of MediaController and VideoView classes, we can play the video files in android.

MediaController class

The android.widget.MediaController is a view that contains media controls like play/pause, previous,
next, fast-forward, rewind etc.

VideoView class

The android.widget.VideoView class provides methods to play and control the video player. The
commonly used methods of VideoView class are as follows:
TEXT TO SPEECH

→ Android allows you convert your text into voice. Not only you can convert it but it also allows
you to speak text in variety of different languages.
→ 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 to implement TextToSpeech.
→ Android provides TextToSpeech class for this purpose. In order to use this class, you need to
instantiate an object of this class and also specify the initListener. Its syntax is given below −
→ Apart from the speak method, there are some other methods available in the TextToSpeech
class. They are listed below-


Example for text to speech-
SENSORS CODE-
ASYNC TASK-

AsyncTask

→ Android AsyncTask is an abstract class provided by Android which gives us the liberty to
perform heavy tasks in the background and keep the UI thread light thus making the application
more responsive.
→ Android application runs on a single thread when launched. Due to this single thread model
tasks that take longer time to fetch the response can make the application nonresponsive.
→ To avoid this we use android AsyncTask to perform the heavy tasks in background on a
dedicated thread and passing the results back to the UI thread.
→ Hence use of AsyncTask in android application keeps the UI thread responsive at all times.
→ 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

→ Audio Capture
→ Android has a built in microphone through which you can capture audio and store it , or play it
in your phone.
→ There are many ways to do that but the most common way is through MediaRecorder class.
→ Android provides MediaRecorder class to record audio or video. In order to use MediaRecorder
class ,you will first create an instance of MediaRecorder class. Its syntax is given below.-

MediaRecorder myAudioRecorder = new MediaRecorder();

→ Apart from these methods , there are other methods listed in the MediaRecorder class that
allows you more control over audio and video recording
CAMERA IN ANDROID

→ In Android, Camera is a hardware device that allows capturing pictures and videos in your
applications.
→ Android provides the facility to work on camera by 2 ways:

1. By Camera Intent 2. By Camera API

1 Using Camera By Using Camera Application

→ We can capture pictures without using the instance of Camera class.


→ Here you will use an intent action type of MediaStore.ACTION_IMAGE_CAPTURE to launch
an existing Camera application on your phone.
→ In Android MediaStore is a type of DataBase which stores pictures and videos in android.

Intent cameraIntent = new


Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

2 Using Camera By using Camera Api

→ This class is used for controlling device cameras.


→ It can be used to take pictures when you are building a camera application. Camera API works
in following ways:

1.Camera Manager: This is used to get all the cameras available in the device like front
camera back camera each having the camera id.
2.CameraDevice: You can get it from Camera Manager class by its id.
3.CaptureRequest: You can create a capture request from camera device to capture images.
4.CameraCaptureSession: To get capture request’s from Camera Device create a
CameraCaptureSession.
5.CameraCaptureSession.CaptureCallback: This is going to provide the Capture session
results.
EXAMPLE FOR CAMERA—
BLUETOOTH
→ Android Bluetooth Tutorial Bluetooth is a way to exchange data with other devices wirelessly.
Android provides Bluetooth API to perform several tasks such as:
o scan bluetooth devices
o connect and transfer data from and to other devices

o manage multiple connections etc.

→ The android.bluetooth package provides a lot of interfaces classes to work with bluetooth such
as:
o BluetoothAdapter
o BluetoothDevice
o BluetoothSocket
o BluetoothServerSocket
o BluetoothClass
o BluetoothProfile
o BluetoothProfile.ServiceListener
o BluetoothHeadset
o BluetoothA2dp
o BluetoothHealth
o BluetoothHealthCallback
o BluetoothHealthAppConfiguration
CODE FOR BLUETOOTH-

Activity_main.xml

<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="24dp"
android:gravity="center_horizontal">
<TextView
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN_ON"
android:layout_marginTop="30dp" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DISCOVERABLE"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN_OFF"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java (Ultra-compact version)
package com.example.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

BluetoothAdapter bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt = BluetoothAdapter.getDefaultAdapter();
if (bt == null) {
findViewById(R.id.out).post(() -> ((android.widget.TextView)
findViewById(R.id.out)).setText("Device not supported"));
return;
}

findViewById(R.id.button1).setOnClickListener(v -> {
if (!bt.isEnabled())
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
});

findViewById(R.id.button2).setOnClickListener(v -> {
if (!bt.isDiscovering()) {
Toast.makeText(this, "Making your device discoverable", Toast.LENGTH_SHORT).show();
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 2);
}
});

findViewById(R.id.button3).setOnClickListener(v -> {
bt.disable();
Toast.makeText(this, "Turning off Bluetooth", Toast.LENGTH_SHORT).show();
});
}
}

AndroidManifest.xml (No changes needed, but cleaned spacing)


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth">

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />


<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

ANDROID SQLITE

→ Android SQLite is a very lightweight database which comes with Android OS.
→ Android SQLite combines a clean SQL interface with a very small memory footprint and decent
speed. For Android, SQLite is “baked into” the Android runtime, so every Android application
can create its own SQLite databases.
→ Android SQLite native API is not JDBC, as JDBC might be too much overhead for a memory-
limited smartphone. Once a database is created successfully its located in data/data//databases/
accessible from Android Device Monitor.
→ SQLite is a typical relational database, containing tables (which consists of rows and columns),
indexes etc. We can create our own tables to hold the data accordingly. This structure is referred
to as a schema.
→ Example

You might also like