Updated 24 November 2017
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,
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.
1 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
and to check the network availability
1 2 3 4 5 6 |
public static boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } |
2. For database
Open or create the database
1 2 3 4 |
public void onCreate(SQLiteDatabase db) { String CREATE_OFFLINE_DATA_TABLE = "CREATE TABLE " + TABLE_OFFLINE_DATA + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, methodname VARCHAR, responsedata VARCHAR, productid VARCHAR, categoryid VARCHAR, profileurlorpageidentifier VARCHAR)"; db.execSQL(CREATE_OFFLINE_DATA_TABLE); } |
here Context.MODE_PRIVATE defines that the database is accessed by that particular app which creates it.
Insert or update into database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public void updateIntoOfflineDB(String methodName, String responseData, String id) { Cursor cursor; try { switch (methodName) { case "mobikulCatalogGetcategoryList": cursor = db.rawQuery("SELECT responsedata FROM " + mTableName + " WHERE methodname = '" + methodName + "'", null); if (cursor.getCount() != 0) { cursor.moveToFirst(); if (cursor.getString(0).equals(responseData)) { Log.d(ApplicationConstant.TAG, "updateIntoOfflineDB: DATA IS SAME"); } else { ContentValues values = new ContentValues(); values.put("responsedata", responseData); String selection = "methodname" + " LIKE ?"; String[] selectionArgs = { methodName }; int count = db.update(TABLE_OFFLINE_DATA, values, selection, selectionArgs); Log.d(ApplicationConstant.TAG, "updateIntoOfflineDB: DATA UPDATED : " + count); } } else { ContentValues values = new ContentValues(); values.put("methodname", methodName); values.put("responsedata", responseData); long newRowId = db.insert(TABLE_OFFLINE_DATA, null, values); Log.d(ApplicationConstant.TAG, "updateIntoOfflineDB: DATA INSERTED AT ROW NUMBER : " + newRowId); } cursor.close(); break; } |
Select from database
1 2 3 4 5 6 7 8 9 |
public Cursor selectFromOfflineDB(String methodName, String id) { Log.d(ApplicationConstant.TAG, "selectFromOfflineDB: No internet connection"); switch (methodName) { case "mobikulCatalogGetcategoryList": return db.rawQuery("SELECT responsedata, methodname FROM " + mTableName + " WHERE methodname = '" + methodName + "'", null); default: return null; } } |
3. Some codes to use in the app
For checking the network connection and performing the functionality according to that
1 2 3 4 5 6 7 8 9 10 11 |
if(Utils.isNetworkAvailable(this)) { new VolleyConnection(this, this).execute("mobikulCatalogGetcategoryList", String.valueOf(Utils.getScreenWidth(this))); } else { Cursor databaseCursor = mOfflineDataBaseHandler.selectFromOfflineDB("mobikulCatalogGetcategoryList", null); Log.d(ApplicationConstant.TAG, "Number of Records found: " + databaseCursor.getCount()); if(databaseCursor.getCount() != 0) { databaseCursor.moveToFirst(); Log.d(ApplicationConstant.TAG, "Data from Database Query: Method Name : " + databaseCursor.getString(1) + ", Data : "+databaseCursor.getString(0)); onTaskCompleted(databaseCursor.getString(0), databaseCursor.getString(1)); } } |
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 … : )
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.