You are on page 1of 8

Mobile Application Development (22617) Practical No.

26

Practical No. 26: Perform Async task using SQLite

I. Practical Significance
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.

II. Relevant Program Outcomes (POs)


PO2- Discipline knowledge
PO3- Experiments and practice

III. Competency and Practical Skills


“Create simple Android applications.”
This practical is expected to develop the following skills
1. Able to develop the application using the Async task.
2. Able to keep the GUI light for heavy database applications.

IV. Relevant Course Outcome(s)


1. Configure Android environment and development tools.
2. Develop rich user Interfaces by using layouts and controls.
3. Create Android application using database

V. Practical Outcome (PrOs)


Demonstrate Async task using SQLite.

VI. Relevant Affective Domain related Outcomes 1. Work


collaboratively in team
2. Follow ethical Practices.

VII. Minimum Theoretical Background


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 non- responsive.
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.

VIII. Resources required (Additional)

Sr. Instrument /Object Specification Quantity Remarks


No.
Android enabled smartphone 2 GB RAM 1 Data cable is
1 / Android version supporting mandatory for
emulator emulators

Maharashtra State Board of Technical Education 1


Mobile Application Development (22617) Practical No. 26

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must design more
such questions to ensure the achievement of identified CO.
1. List the basic methods used in an android AsyncTask class.
2. Differentiate between AsyncTask and Services.
3. Name the method used, if a process takes a long time to do its work?

(Space for answers)


1.
abstract Result doInBackground(Params... params)
void onPreExecute()
void onPostExecute(Result result)
void onProgressUpdate(Progress... values)

2.
Android defines AsyncTask as “a class that extends the Object class to allow short operations
to run asynchronously in the background.”
Services are used for long running operations.

3.
Use AsyncTask or Service in background for a process that takes a long time to do its work.

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to insert data in SQLite database using AsyncTask

(Space for answers)

Answers:
1.
//MainActivity.java

package com.jamiapolytechnic.experiment261;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.AsyncTask;

Maharashtra State Board of Technical Education 2


Mobile Application Development (22617) Practical No. 26

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


Database_SpeedDial dbObj;
ArrayList<String> alContactName, alPhoneNumber;
TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alContactName = new ArrayList<String>();
alContactName.add("AA");
alContactName.add("BB");
alContactName.add("CC");
alPhoneNumber = new ArrayList<String>();
alPhoneNumber.add("11");
alPhoneNumber.add("22");
alPhoneNumber.add("33");
message = findViewById(R.id.message);
}

public void process(View v) {


switch (v.getId()) {
case R.id.btn_addcontact_add:
// Using AsyncTask
// alImage, alContactName ,.... are ArrayList<String>
InsertData id = new InsertData(alContactName, alPhoneNumber);
id.execute();
message.setTextColor(Color.GREEN);
message.setText("Data inserted successfully");

Maharashtra State Board of Technical Education 3


Mobile Application Development (22617) Practical No. 26

Toast.makeText(getApplicationContext(),"Finished Inserting", Toast.LENGTH_LONG)


.show();
break;
case R.id.btn_addcontact_cancel:
message.setTextColor(Color.RED);
message.setText("Cancelled");
Toast.makeText(getApplicationContext(),"Cancelled", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
private class InsertData extends AsyncTask<Void, Void, Void> {
ArrayList<String> alContactName_insertdata;
ArrayList<String> alPhoneNumber_insertdata;

public InsertData(ArrayList<String> alName,


ArrayList<String> alNumber)
{
alContactName_insertdata = alName;
alPhoneNumber_insertdata = alNumber;
}

@Override
protected void onPreExecute() {
}

@Override
protected Void doInBackground(Void... params) {
dbObj = new Database_SpeedDial(MainActivity.this);

for(int i=0; i<alContactName_insertdata.size(); i++)


{
dbObj.insert_row(alContactName_insertdata.get(i),
alPhoneNumber_insertdata.get(i));

Maharashtra State Board of Technical Education 4


Mobile Application Development (22617) Practical No. 26

}
return null;
}
@Override
protected void onPostExecute(Void res) {
//System.out.println("Inside onPostExecute");
}
}
}

//=====================================================================
//activity_main.xml

<?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"
android:orientation="vertical"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:layout_marginTop="50dp">

<Button
android:id="@+id/btn_addcontact_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Contact"
android:gravity="center"
android:onClick="process" />
<Button
android:id="@+id/btn_addcontact_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"

Maharashtra State Board of Technical Education 5


Mobile Application Development (22617) Practical No. 26

android:gravity="center"
android:onClick="process" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="100dp"
android:textColor="#0000ff"
android:text="Messages here"/>

</LinearLayout>

//=====================================================================
//Database_SpeedDial.java

package com.jamiapolytechnic.experiment261;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Database_SpeedDial extends SQLiteOpenHelper{
static String name = "speeddial_contacts";
static int version = 1;
SQLiteDatabase db;

public Database_SpeedDial(Context context) {


super(context, name, null, version);
db = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS cntcts(name TEXT, number TEXT)");

Maharashtra State Board of Technical Education 6


Mobile Application Development (22617) Practical No. 26

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public void insert_row(String name, String number) {


db.execSQL("INSERT INTO cntcts VALUES('"+ name +"', '"+ number +"')");
}
}

Maharashtra State Board of Technical Education 7


Mobile Application Development (22617) Practical No. 26

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage

Process related (10 Marks) 30%

1. Logic Formation 10%


2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%

4. Interactive GUI 20%


5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

List of student Team Members


1
2
3
4

Marks Obtained Dated signature of


Teacher
Process Product Total
Related(10) Related(15) (25)

Maharashtra State Board of Technical Education 8

You might also like