Read complete story

How to make your application work even in offline mode

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 … : )

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close