The Wayback Machine - https://web.archive.org/web/20240902075635/https://www.geeksforgeeks.org/behaviour-components-of-android-jetpack/
Open In App

Behaviour Components of Android Jetpack

Last Updated : 16 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single modular entity. Nowadays, nearly 99% of the apps present on the Google Play Store uses Android Jetpack libraries. The behavior area of the android jetpack covers those libraries that enable users to interact with the application through the UI. This component integrates the standard Android services like notification, downloading, permissions, sharing, assistant, etc. This article explains each and every library of the Behavior component in detail. Jetpack consist of a wide collection of libraries that are built in a way to work together and make robust mobile applications. Its software components have been divided into 4 categories:

  1. Foundation Components
  2. Architecture Components
  3. Behavior Components
  4. UI Components

Further, the following are the list of all Behavior components:

  1. DownloadManager
  2. Media & Playback
  3. Permissions
  4. Notifications
  5. Sharing
  6. Slices

Ways to include Android Jetpack libraries in the application

  • Add google repository in the build.gradle file of the application project.

allprojects {

 repositories {

     google()

     jcenter()

     }

}

  • All Jetpack components are available in the Google Maven repository, include them in the build.gradle file

allprojects {

 repositories {

     jcenter()

     maven { url ‘https://maven.google.com’ }

      }

}

Behavior Components

1. Download Manager

The DownloadManager is a system service in Android that helps in downloading bulky files in the background thread. The ability of mobile devices to download and store files locally is very important and necessary because it is not very appealing to users to keep their internet on all the time. Moreover, continuous internet usage drains the battery of devices faster and leads to increased cost. DownloadManager class handle only HTTP downloads, and it is responsible for avoiding connectivity problems, for resume downloading if the device reboot and mechanism to retry if the file crashes. Since it is a system service, users can simply start a download and listen for a broadcast event to handle the finished download.

a. Initializing the DownloadManager

Like any other system service, the DownloadManager is initialize using the getSystemService() method. The resultant object is then cast into the DownloadManager class.

private void initializeDownloadManager() {

       downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

   }

b. Creating a download request

The HTTP request of the user is defined in the Request class which is an inner class of DownloadManager.  

DownloadManager.Request request=new 

DownloadManager.Request(Uri.parse(“https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200x200-min.png”));

       request.setTitle(“GfG_logo”)

               .setDescription(“File is downloading…”)

               .setDestinationInExternalFilesDir(this,

                       Environment.DIRECTORY_DOWNLOADS,fileName)

               .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

c. Enqueue a Download

To enqueue a download(adding a download request to the queue of the download manager), the enqueue() method is used. This queue is processed automatically by the system and it returns a download ID.

downLoadId=downloadManager.enqueue(request);

d. Remove/Delete a downloaded file:

In order to remove/delete a downloaded file from the download manager, the remove() method is called and the download ID of the file is passed into it as an argument.  

  private void deleteDownloadedFile(){

       downloadManager.remove(downLoadId);

   }

2. Media & Playback

Jetpack provides a backward-compatible API for the Android multimedia framework. The included Media libraries facilitate developers to integrate audio, video, and image files into an application. Further, the Playback libraries allow android apps to playback video and audio using sessions and media controllers. The MediaPlayer APIs can access and play media files from the application’s resources(raw resources), from standalone files in the filesystem, or from a data stream coming through a network connection. Below mentioned classes are used to play sound and video in the Android framework:

  • MediaPlayer: Primary API responsible for playing audio and video
  • AudioManager: Manage audio sources and audio output(volume control) on the device.

The Android framework provides a variety of options to the developers on the use of the type of media players in an application:

  • Media Player: This is a clean player having basic functionalities and it supports the most commonly used audio-video formats as well as data sources. The simple and easy to use interface of this player makes it suitable in many uses cases; however, it supports minimal customization.
  • ExoPlayer: An open-source software that supports high-performance features and adaptive streaming technology like DASH and HLS.
  • YouTube: For those use cases in which an app plays videos precisely from the YouTube platform, developers consider integrating this API.
  • Custom Media Player: In order to design a fully customized media player that fulfills the exact need for an application, one can use low-level media APIs such as MediaCodec, MediaDRM, and AudioTrack.

To design an application using MediaPlayer, some appropriate declaration has to be made in the manifest file to use the associated features.

a. To stream any network-based content using MediaPlayer, the app must request network access

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

b. To prevent the screen from dimming or the processor from sleeping while an application is running a MediaPlayer, the app must request Wake Lock permission

<uses-permission android:name=”android.permission.WAKE_LOCK” />

3. Permissions

This area of the Behavior component accommodates a permission system along with a set of predefined permissions for certain tasks in the Android apps. Almost every application request some permissions from the user. For example, if an application requires network access to carry out a task, it will define permission for the user. Developers declare the permissions for an application in its manifest file and the code must handle both the situations i.e., acceptance as well as denial of the requested permission by the user. Moreover, this file can also define additional permissions that will be used to restrict access to particular components.

The concept of asking permission in the Android system has changed significantly after API 23. Initially, the applications ask for all the permissions from the user at the time of installation. After the release of API level 23, the applications seek permissions during runtime. With this new model of permission in the Android system, users can grant one-time permission to an app for a certain task rather than selecting “allow always”. There are different levels present in the system permissions, one of them is protection levels. Following are the two important protection level for any permission:

  1. Normal: The set of permissions that falls in this category are safe in terms of users’ privacy as well as carrying out tasks that require the involvement of other applications. This type of permission is granted to the application by default. To set a time zone for a device/application is an example of normal permissions.
  2. Dangerous: This class of permissions targets user’s private information, for example, permission to read user’s contact data. These permissions have the potential to affect the operations of other applications as well. Generally, the app asks for dangerous permissions during runtime.

Permission groups: Dangerous permissions that seek the user’s choice to grant or deny it are classified into 9 groups. The purpose of doing such type of grouping is to facilitate users to grant all the permissions required by a component with a single action instead of selecting one by one. For example, it is convenient for users to grant all permissions related to the editing of contacts at once rather than granting access separately to view, edit, and add contacts. Following is the table of permission groups:

Permission

Group Description

Calendar Managing calendars
Contacts Managing contacts
Location Current device location
Camera Taking photos and recording videos
Phone Dialing and managing phone calls
SMS Sending and viewing messages
Microphone Audio recording
Storage Accessing photos, media, and files
Body Sensors Pulse rate, heart rate, and similar kind of data

4. Notifications

Generating a notification is one of the most beneficial features of today’s mobile applications. Its prime purpose is to inform the users regarding events happening inside an app. By using it in the correct way, this utility has the capability to elevate user’s motivation on using the application. Android provided the Notification service from the very beginning and it has evolved continuously over the period of time. Developers can accommodate various kinds of images and buttons in the notification area which makes them more expressive. Not only mobile phones but Android TV and wearables also use the notification feature in order to control their media operations. Following are the types of notifications that are generally used:

  1. Communication from other users
  2. Well-timed and informative task reminders

Ways to inform the users: The device can attract the user’s attention and can inform them regarding the arrived notifications. Following are the ways to do the same:

  • Play a sound or vibrate
  • Show a status bar icon
  • Display notification on the lock screen
  • Blink the device’s LED
  • Notification can peek onto the current screen

5. Sharing

In today’s world, humans are very much dependent on mobile applications. Users need to share the day to day important information with their colleagues, family, or friends. This information can be in the form of text, images, or other document files. Thus it is extremely important for the Android apps to have the ability to communicate and integrate with each other. The Sharing division of Behavior component is responsible for sharing and receiving contents with different applications. The ShareActionProvider class is used to carry out the task of sharing contents and information. 

In order to ease the procedure of sharing information across various applications, Android uses Intents and their associated extras. In Android, there are two ways for users by which they can share data between apps:

a. Using Android Share sheet: It is a dialog box that appears on the screen when a user makes a share action request. All the apps available on the device and are compatible with the sharing feature appears on the screen in a sheet-like structure. The main aim of this share sheet is to send information/content outside an app and/or directly to another user. 

b. Using Android Intent Resolver: It is mainly used for sharing a file within different applications available on the device. For example, opening an email file on the device and asking the user to choose their preferred mail application.

6. Slices

Android provides a new technique to display the remote contents in the form of Slices. It is the UI component that displays the contents from an application to the Google Search app or in different platforms like Google Assistant or Google Assistant devices. Being a part of Jetpack, it has backward compatibility up to Android 4.4(API 19). Developers find this module very beneficial because by making app data available using Slices, a user can find information about that app by using Google Search or Assistant. Further, Android Jetpack facilitates in creating flexible UI templates using Slices that is capable of displaying app data outside the app. 

In the Android project, SliceProvider class allows an application to produce content that will be displayed in system spaces. SliceProvider is the extension of Content Provider and Slices are constructed on the top of the Content providers. Developers can host a variety of slices from an application as it is based on the content URIs. The application will receive a content URI and then select the kind of slice needed to build and present in front of a user. The onBindSlice() method is called when an application needs to display the slice. This method returns the slice-based upon the content URI received as an input. To update the slice notifyChange() method is called. Android search applications like Google Assistant or Google Search are termed as SlicePresenter. Slices are fetched by SlicePresenter by calling System API along with the Slice URI. 



Previous Article
Next Article

Similar Reads

Jetpack Architecture Components in Android
Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single modular entity. Nowadays, nearly 99% of the apps
10 min read
Foundation Components of Android Jetpack
Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single modular entity. Nowadays, nearly 99% of the apps
7 min read
UI Components of Android Jetpack
Android Jetpack is a set of software components, libraries, tools, and guidance to help in developing robust Android applications. Launched by Google in 2018, Jetpack comprises existing android support libraries, android architecture components with an addition of the Android KTX library as a single modular entity. Nowadays, nearly 99% of the apps
14 min read
Android Runtime Permissions with Dexter using Android Jetpack Compose
Android applications require the usage of hardware devices within the android applications such as microphones or cameras. For using these devices within any android application. Permissions for using this hardware have to be provided to the application. For giving these permissions to the application runtime permissions are used. Handling permissi
7 min read
Horizontal ListView in Android using Jetpack Compose
Many Android Applications present data in the form of horizontally scrollable lists. This list can be scrolled horizontally. In this article, we will take a look at How to Implement Horizontal ListView in Android Applications using Jetpack Compose. Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only avai
6 min read
Material Design Text Input Field using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
3 min read
How to Remove TextField Padding in Android using Jetpack Compose?
In Android Jetpack Compose, a TextField is used to take input from the user in the form of text. TextField when in focus or when clicked invokes a soft keyboard. In general, TextField is highly attributed for a better feel and appearance. However, we can use a pre-developed TextField, named BasicTextField, which has no attributes like that of a reg
3 min read
WebView in Android using Jetpack Compose
In Android, a WebView is an extension of View Class that allows us to display webpages. In simpler words, if you want to display a webpage in Activity Layout, then you can implement a WebView to display it. It is very similar to a browser but isn't one. A WebView can rather be referred to as a show or a preview of a browser as it lacks most functio
3 min read
Motion Layout Button in Android Jetpack Compose
Motion Layout is a special version of Constraint layout. With the help of motion layout, we can add animations to the widgets within the layout and change the position of that widget dynamically. In this article, we will take a look at How to implement Motion Layout animation on buttons in Android using Jetpack compose. What we are going to build i
8 min read
Play Audio in Android using Jetpack Compose
In Android, a MediaPlayer is used to play media files like audio and video files in the application. The MediaPlayer Class uses the MediaPlayer API for performing various functions like creating a MediaPlayer, playing, pausing, starting, and stopping the media. In this article, we will show you how you could play an Audio File on Android using Jetp
3 min read
Android Jetpack - WorkManager with Example
WorkManager is an Android Jetpack library that provides a flexible and efficient way to schedule and manage background tasks in your app. It is a powerful and recommended tool for managing background processing in Android applications. With WorkManager, you can schedule asynchronous, deferred, and periodic tasks that need to run even when the app i
5 min read
Curved Text in Android using Jetpack Compose
In Android, there are no inbuilt schemes to implement designed text like the word art, which is available in applications like MS Word. We can always refer to or download such schemes like designed font faces to implement designed text. However, it is also possible to natively build (building from scratch) such designed texts in an Android Project.
3 min read
How to Get Screen Width and Height in Android using Jetpack Compose?
Android applications are being developed for different device orientations to support a huge range of devices. So that users with different device size configurations can use the application. Many applications need to get the height and width of the device screen to create UI. In this article, we will take a look at How to get Screen Width and Heig
5 min read
Extract Data From PDF File using Android Jetpack Compose
PDF files are used in many android applications for displaying data in the form of images, text as well as graphics. Many applications are also required to get the data from this PDF file and display this data within the android application. So for extracting the data from PDF file pdf reader is used which is used to read pdf and get the data from
6 min read
Introduction to Android Jetpack
The Android support libraries are used in almost all android applications to overcome the compatibility issues across different Android OS versions and devices. These libraries also facilitate users to add various kinds of updated widgets in the application. Over time, these libraries are updated according to the latest android versions. The proble
6 min read
How to Create a New Project in Android Studio Canary Version with Jetpack Compose?
Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. After successful installation of the Android Studio Canary version, we are moving towards the creation of a new Project. In this article, we will take a look at How to create a new project in the Android Studio Canary Version using Jetpack Compos
2 min read
Jetpack Compose Navigation and Passing Data in Android
Almost every app uses some kind of navigation, allows users to move from one screen to another. In this article, we will learn to implement Navigation in Jetpack Compose using Compose way. We will build a simple app demonstrating Jetpack compose navigation, It will have three screens(Home, Profile, and Settings). It will navigate to Settings Screen
4 min read
Basics of Jetpack Compose in Android
Jetpack Compose is a modern UI toolkit that is designed to simplify UI development in Android. It consists of a reactive programming model with conciseness and ease of Kotlin programming language. It is fully declarative so that you can describe your UI by calling some series of functions that will transform your data into a UI hierarchy. When the
5 min read
TopAppBar in Android using Jetpack Compose
TopAppBar is similar to that of the Action Bar widget in Android. This is one of the most UI components in Android. The action bar is used to represent the app name and action items in Android. In this article, we will take a look at the implementation of the TopAppBar in Android using Jetpack compose. Attributes of Top App Bar WidgetAttributes Des
3 min read
View Binding in Android Jetpack
View Binding is one of the best features which provides the views to bind with the activity which is ongoing. Replacing the findViewById() method, hence reducing the boilerplate code, generated the instances of the views of the current layout. And most important feature about the View Binding is it's always null safe. In this article detailed it's
4 min read
View Binding with Fragments in Android Jetpack
In the Previous article View Binding in Android Jetpack, it's been discussed why acquiring the ViewBinding feature in the Android project provides a lot of benefits. But when it comes to ViewBinding with fragments the scenario changes. Because the lifecycle of the Fragment is different and that of activity is different Here also things go the same
6 min read
How to Draw Track on Google Maps in Android using Jetpack Compose?
In many android apps, we have seen that there is a route marker from a source location to the destination location. In this article, we will take a look at How we can draw a track on Google Maps in Android using Jetpack Compose. Step By Step ImplementationStep 1: Create a New Project in Android StudioTo create a new project in Android Studio please
6 min read
Material Design Buttons using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
4 min read
Snackbar in Android using Jetpack Compose
Prerequisites: Snackbar Material Design Components in AndroidJetpack Compose in Android Snackbar is a lightweight widget and they are used to show messages at the bottom of the application. It was introduced with the Material Design library as a replacement for a Toast. In this article, we will explain how you can create a Snackbar using Jetpack Co
2 min read
Custom Chips using Jetpack Compose in Android
Chips in android are one of the components which are used to make the choice filters, actions, and display the selectable options in the compact area of the Android Window. In this article, we will use Android's Jetpack Compose to create those chips. A sample image is given below to give an idea of what we are going to build. Note that we are going
5 min read
Android Jetpack Compose - How to Use Phone Selector API
In most android applications, we get to see that users don't have to enter their phone numbers for authentication. A pop-up message is triggered automatically which displays the phone number of the user which is present on the device. This feature helps users to easily authenticate within the application and reduces the efforts. In this article, we
5 min read
How to make Text Selectable in Android using Jetpack Compose?
By default, Composable Texts are not selectable which means users cannot copy text from your application, and to enable text selection you need to wrap the text elements into Selection Container. In this article, we will use Android’s Jetpack Compose to create those chips. A sample image is given below to give an idea of what we are going to build.
2 min read
How to Create a Timer using Jetpack Compose in Android?
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. In this article, we are going to create a Timer using Jetpack Compose. Below is the sample video to show what we are going to build. [video mp4="https://m
4 min read
ConstraintLayout in Android using Jetpack Compose
ConstraintLayout is the view system in a ViewGroup to place widgets relative to the position of other widgets on the screen. In jetpack compose, almost every layout can be created using Rows and Columns but if you want to create larger complicated Layouts you can also use Constraint Layout as an alternative to Rows and Columns. In this article, we
5 min read
How to Create Superscript and Subscript Text using Jetpack Compose in Android?
In this article, we will explain how to create a Superscript and Subscript text using Jetpack Compose. Below is the sample picture to show what we are going to build. Note that we are going to implement this project using the Kotlin language. Prerequisites: Basic Knowledge of Kotlin.Jetpack Compose in Android.Step by Step Implementation Step 1: Cre
2 min read