You are on page 1of 17

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

JNANA SANGAMA, BELAGAVI – 590018

A Project Report
For the 6th semester

“ASYNCHRONOUS TASK”
Submitted in partial fulfillment of the requirements for the award of degree
of
BACHELOR OF ENGINEERING
IN
COMPUTER SCIENCE AND ENGINEERING

Submitted by

Shrushti S Ambudkar 2VD19CS050


Spoorti S Naik 2VD19CS053
Srushti M Kandagal 2VD19CS055

Under the Guidance of


Asst.Prof.Pranesh Kulkarni

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


KLS VISHWANATHRAO DESHPANDE INSTITUTE OF
TECHNOLOGY, HALIYAL-581329
2020-2021
KLS VISHWANATHRAO DESHPANDE
INSTITUTE OF TECHNOLOGY, HALIYAL - 581329

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Certificate
Certified that the Project work entitled “Asynchronous Task” is bonafide work carried
out Shrusti S Ambudkar (2VD19CS050), Spoorti S Naik (2VD19CS053), Srushti M Kandagal
(2VD19CS055) in partial fulfillment of the requirements for the award of the degree of
Bachelor of Computer Science and Engineering of Visvesvaraya Technological
University, Belagavi, during the year 2020-2021. It is certified that all the
corrections/suggestions indicated for internal assessment have been incorporated in the report.
The project report has been approved as it satisfies the academic requirements in respect of
project work prescribed for the Bachelor of Engineering degree.

Signature of the Guide Signature of the HOD Signature of the Principal


Asst.Professor Prof. Poornima Raikar Dr. V. A Kulkarni
Pranesh Kulkarni

Name of the Examiners: Signature with date:


TABLE OF CONTENTS

Acknowledgement i

Abstract ii

Index iii
ACKNOWLEDGEMENT

“Task successful” makes everyone happy. But the happiness will be gold without glitter if we
didn’t state the persons who have supported us to make it success. Success will be crowned to
people who made it reality but the people whose constant guidance and encouragement made
it possible will be crowned first on the eve of success.

We wish to express our sincere gratitude towards our guide Prof.Pranesh Kulkarni for their
constant motivation and valuable help through the project.

We take this opportunity to thank Prof.Poornima Raikar HOD of Computer Science and
Engineering Department for providing the inspiration for taking the project to its completion.

We are also grateful to our beloved principal Dr.V.A. Kulkarni for having provided us the
academic environment, which nurtured our practical skills, contributing to the success of our
project. We would like to thank all the teaching and non-teaching for their ideas and
encouragement which helped us through the completion of the project.

We extend our sincere gratitude towards our parents who have encouraged us with their
blessings to do this project successfully.
ABSTRACT

In this project, we are demonstrating the use of Asynchronous tasks in android. This
Asynchronous task will implement the functionality of a simple moving banner and also, we
will get know that what’s going to do background operation on background thread and update
on main thread. In android we can’t directly touch background thread to main thread in android
development. Asynctask help us to make communication between background thread to main
thread.
INDEX

Page number

Chapter 1 Introduction 1-2

Chapter 2 Literature Survey 3

Chapter 3 System Requirement Specification 4

Chapter 4 Implementation 5-7

Chapter 5 Results 8 -11


Conclusion and Future Work References
Asynchronous Task 1

Chapter 1
INTRODUCTION

In Android like in others platforms, it can be useful to use asynchronous tasks to process long
operations like download a file or consume a web service. In Java, you know that the entry
point is the Thread class. Android using a part of Java API, you can also use Thread class but
it’s to you to manage synchronization with the main thread for example and you have no
possibility to cancel a thread.

A more easy way is provided by Android SDK: the AsyncTask class. The AsyncTask class
encapsulates the creation of a background process and the synchronization with the main
thread. It also supports reporting progress of the running tasks.

In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background
and then synchronize again with our main thread. This class will override at least one
method
i.e. doInBackground (Params) and most often will override second method onPostExecute
(Result).

AsyncTask class is used to do background operations that will update the UI (user interface).
Mainly we used it for short operations that will not effect on our main thread.

AsyncTask class is firstly executed using execute () method. In the first step AsyncTask is
called onPreExecute () then onPreExecute () calls doInBackground () for background processes
and then doInBackground () calls onPostExecute () method to update the UI.

By default, our application code runs in our main thread and every statement is therefore
execute in a sequence. If we need to perform long tasks/operations then our main thread is
blocked until the corresponding operation has finished. For providing a good user experience
in our application we need to use AsyncTasks class that runs in a separate thread. This class
will execute everything in doInBackground () method inside of other thread which doesn’t have
access to the GUI where all the views are present. The onPostExecute () method of this class
synchronizes itself again with the main UI thread and allows it to make some updating. This
method is called automatically after the doInBackground method finished its work.

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 2

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.

There are a few threading rules that must be followed for this class to work properly:

1. This class must be loaded on the UI thread. This is done automatically as from
JELLY_BEAN.

2. The task instance must be created on the UI thread.

3. execute (Params…) method that executes it, must be invoked on the UI thread.

4. Do not call onPreExecute (), onPostExecute (Result), doInBackground (Params…),


onProgressUpdate (Progress…) manually, just executes the class and then will call
automatically for good user experience.

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 3

Chapter 2
LITERATURE SURVEY

In the recent years, the advances in mobile technology have brought an exorbitant change in
daily lifestyle of individuals. Smartphones/mobile devices are rampant in all aspects of human
life. This has led to an extreme demand for developing software that runs on mobile devices.
The developers have to keep up with this high demand and deliver high-quality app on time
and within budget. For this, estimation of development and testing of apps play a pivotal role.
A Systematic Literature Review (SLR) is conducted to highlight development and testing
estimation process for software/application. The goal of the present literature survey is to
identify and compare existing test estimation techniques for traditional software
(desktop/laptop) and for mobile software/application. The characteristics that make mobile
software/application different from traditional software are identified in this literature survey.
Further, the trend for developing the software is towards agile, thus this study also presents and
compares estimation techniques used in agile software development for mobile applications.
The analysis of literature review suggests filling a research gap to present formal models for
estimating mobile application considering specific characteristics of mobile software.

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 4

Chapter 3
SYSTEM REQUIREMENT SPECIFICATION

Hardware Requirements

 Intel Pentium CPU 2.6 GHz or AMD Athlon 64 (K8) 2.6 GHz or higher
 1 GB RAM or more
 Mouse
 Keyboard 108 standard
 Monitor resolution 800×600

Software Requirements:

 Programming Language: JAVA/ Kotlin


 Operating System: Windows
 IDE: Android Studio
 Android SDK

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 5

Chapter 4

IMPLEMENTATION

In Android, AsyncTask is executed and goes through four different steps or method. Here are
these four methods of AsyncTasks.

1. OnPreExecute () – It invoked on the main UI thread before the task is executed. This
method is mainly used to setup the task for instance by showing Progress
Bar or Progress Dialog in the UI (user interface).

@Override
Protected void onPostExecute (Void avoid) {
// this is where you get the result after running in the background
super. OnPostExecute(aVoid);
callbacks. onDone ();
}
2. doInBackground (Params) – This method is invoked on the background thread
immediately after onPreExecute () finishes its execution. Main purpose of this method
is to perform the background operations that can take a long time. The parameters of
the Asynchronous task are passed to this step for execution. The result of the operations

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 6

must be returned by this step and it will be passed back to the last step/method i.e.
onPostExecutes (). This method can also use publishProgress (Progress…) to publish
one or more units of progress. These values will be published on the main UI thread in
the onProgressUpdate (Progress…) method.
/**
* This method is called to do stuff in the background
*
* @param strings this the first params passed in the typed params of the class
**/
@Override
protected Void doInBackground (String... strings) {
// do the heavy stuffs in here
callbacks. do Execute ();
return null;
}
3. onProgressUpdate (Progress…) – This method is invoked on the main UI thread after
a call to publishProgress (Progress…). Timing of the execution is undefined. This
method is used to display any form of progress in the user interface while the
background operations are executing. We can also update our progress status for good
user experience.
@Override
protected void onProgressUpdate (String... values) {
super. OnProgressUpdate(values);
//this is where you update the items or ui on the main thread from the background works
}
4. onPostExecute (Result) – This method is invoked on the main UI thread after the
background operation finishes in the doInBackground method. The result of the
background operation is passed to this step as a parameter and then we can easily update
our UI to show the results.
@Override
protected void onPostExecute (Void aVoid) {
// this is where you get the result after running in the background
super. OnPostExecute(aVoid);

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 7

Callbacks. On Done ();


}

Steps to Implement:

Step 1: Create a new project and name it AsyncTasksExample.

Step 2: Open build. grade and add Picasso library dependency.

Step 3:Open res -> layout ->activity_main.xml (or) main.xml and add code:

Step 4: Open src -> package -> MainActivity.java

In this step firstly we get the reference of Button, Text View’s and Image View. After
that we perform setOnClickListener event on button and execute AsyncTasks class. In
AsyncTasks class firstly we display a Progress Dialog in onPreExecute Method then we
implement the API in doInBackground method and after getting response in postExecute
method we simply parse the JSON data and display the title, category and Image in UI. In this,
we also used Picasso Library to fetch the image from URL.

Step 5: Open AndroidManifest.xml file and add Internet Permission.

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 8

Chapter 5

RESULTS

(a)Asynchronous task

(b)Asynchronous task started

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 9

(c)Asynchronous task finished

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 10

CONCLUSION

AsyncTask enables proper and easy use of the UI thread. This class allows to perform
background operations and publish results on the UI thread without having to manipulate
threads and/or handlers. It is also intended to allow proper and easy use of UI thread, however,
its integrations with the UI thread causes memory leaks and missed callbacks and inconsistent
behaviors on different versions of a platforms from doInBackground method. AsyncTask is
designed ultimately to be a helper around Thread and Handlers and does not consist of the
generic threading frameworks. This is helpful when the task is to last for few seconds but if the
task is going to last long then it will be advisable to use a worker thread.

Dept CSE, KLS VDIT Haliyal


Asynchronous Task 11
REFERNCES

[1].https://abhiandroid.com/programming/asynctask
[2].https://www.geeksforgeeks.org/
[3].https://booksoncode.com/

Dept CSE, KLS VDIT Haliyal

You might also like