Android Basics – Learn how to use an AsyncTask to create a background thread

Like you have seen earlier the code written in the MainActivity class runs in the main thread. If you run the database related code like insert/update etc. on the MainThread then it will slow the App and can lead to bad user experience.

AsyncTask class helps us to overcome this by creating a background thread. Let’s see how to use this.

An AsyncTask has the following methods

  • onPreExecute() : Runs on the main thread and allows you to do initial setup
  • doInBackground() : Runs on a background thread.
  • onPostExecute() : Runs on the Main Thread after the task completes.

Refer to the code segment below in which an inner AsyncTask has been created.

private class SurveyDBAsyncTask extends AsyncTask<String, Void, Long>{

    ContentValues cv;

    protected void onPreExecute() {
        Log.v(TAG, "Data entered by the user is "+userNameId.getText()+" "+userAgeId.getText()+" "+userEmailId);

        cv = new ContentValues();
        cv.put(SurveyDBHelper.SURVEY_TABLE_NAME_COLUMN, userNameId.getText().toString());
        cv.put(SurveyDBHelper.SURVEY_TABLE_EMAIL_COLUMN, userEmailId.getText().toString());
        cv.put(SurveyDBHelper.SURVEY_TABLE_AGE_COLUMN, userAgeId.getText().toString());


     * Override this method to perform a computation on a background thread. The
     * specified parameters are the parameters passed to {@link #execute}
     * by the caller of this task.
     * <p/>
     * This method can call {@link #publishProgress} to publish updates
     * on the UI thread.
     * @param params The parameters of the task.
     * @return A result, defined by the subclass of this task.
     * @see #onPreExecute()
     * @see #onPostExecute
     * @see #publishProgress
    protected Long doInBackground(String... params) {
        long id = 0;

        try {
            SurveyDBHelper surveyDBHelper = new SurveyDBHelper(MainActivity.this);
            SQLiteDatabase db = surveyDBHelper.getReadableDatabase();

            id = db.insert(SurveyDBHelper.SURVEY_TABLE, null, cv);

        }catch (SQLiteException e){
            Log.v(TAG, "Exception "+e.getMessage());
        return id;

    protected void onPostExecute(Long id) {

You can find the complete source code here.

Android Basics – Introduction to SQLite Database

Why you need a Database?

A scenario can be if your app needs to store data. Like when a user registers then you can save the information for later use.

There can be other reasons also. For example if a user has weak or no network then your app can display the last saved data to the user.
What is SQLite Database?

Android comes with a lighweight database which can be used with your apps. SQLite is an open source lighweight database.

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

Why use SQLite?

1. It’s lightweight – It uses a file to store data. So no processing time is needed and hence you conserve battery.

2. It’s fast and stable and can handle transactions.

3. It’s optimized for a single user.

How does SQLite store data?

Android creates a data folder for each app where the files reside. Two files are created to store the data.


This folder can only be accessed by the App.

Reference –