How to make your application work even in offline mode

Updated 24 November 2017

Save

In this blog, we will learn about implementing a very awesome feature in the application which is Offline Mode. Making an application work in offline mode which always works only with network availability is a cool stuff to do.

If we talk about the approaches to do this, we can definitely have two, three or maybe more.

Some of them are,

  1. Using Shared Preferences
  2. Using SQLite Database
  3. Using you Cache Memory

 

We will discuss the 2nd approach which is using SQLite Database.

 

For creating database and tables in it Android provides SQLite database classes with the help of which you can create or manipulate your database.

 

In this approach, all you need to do is check whether the network is available or not and do the functionality according to that.

 

Like if the network is available we will create the HTTP connection to get the data from a server and insert the response into a database. As the app user explores the pages the server response will get stored in the database for offline mode.

 

And in the case of the network is unavailable then instead of creating HTTP connection, the app should load the data from the database.

 

Below you will find some code segments which will help you to perform the database queries and network check.

 

You need to extend SQLiteOpenHelper to be able to use some of the given functions

 

      1. For Network check

Add permission to access network state.

and to check the network availability

 

2. For database

Open or create the database

here Context.MODE_PRIVATE defines that the database is accessed by that particular app which creates it.

 

Insert or update into database

 

Select from database

 

3. Some codes to use in the app

For checking the network connection and performing the functionality according to that

 

These are some of the codes for which can be helpful in understanding the functionality and obviously you can create your own to work according to your need.

 

That all !!! If you need more information you can always contact me …

 

Thank you very much, This is Vedesh Kumar signing off … : )

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home