Skip to content
Related Articles

Related Articles

Improve Article

Behaviour Components of Android Jetpack

  • Difficulty Level : Medium
  • Last Updated : 25 Feb, 2021

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:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
  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 AudoTrack.

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

CalendarManaging calendars
ContactsManaging contacts
LocationCurrent device location
CameraTaking photos and recording videos
PhoneDialing and managing phone calls
SMSSending and viewing messages
MicrophoneAudio recording
StorageAccessing photos, media, and files
Body SensorsPulse 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. Andriod 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. 




My Personal Notes arrow_drop_up
Recommended Articles
Page :