Notification Resender. Part 3

Notification Resender - allows Zeroner to resend notifications from all apps

Hi again!
Today I will continue my series of posts about creating Android app for resending notifications. First part you can read here:  http://dbondarchuk.com/2016/09/22/notification-resender-part-1/. Second – http://dbondarchuk.com/2016/09/26/notification-resender-part-2/

Today’s post will be about working with databases in Android app.

Why we need database in our mobile app? We need to store information that we need from launch to launch, like settings, profiles etc. There are different types to store data in Android:

  1. Shared Preferences
  2. Internal Storage
  3. External Storage
  4. SQLite Databases
  5. Network Connection

Shared preferences are like INI files in Windows, but they are hidden from other apps and user. Internal storage – saves files in private folder (hidden from other apps and user). External storage – just saves a file in public folder, like SD card, Device memory etc. And network connection just triggers web calls (for example, get/post JSON with data).

But the most interesting and very useful type is SQLite Databases. Android Developer Guide says:

Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application.

The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database.

The main idea is that we can get readable or writable database, run some query and get a cursor to navigate through results (row by row). Okay, let’s start creating our database 🙂

First of all, create a Java class named DbHelper. It will contain all needed functions to work with database:

This will create a inherited class, that will work with database “settingsDB” and version number #1. On first call to this database, it will create a new table in this database with name “settings” with columns that will match with properties from our class ResendSetting. In case, if we need to add/change/remove some column in table, we have function onUpgrade, where we can do needed manipulations basing on oldVersion and newVersion.

Okay, we have a table, now we need to read saved settings from it. Let’s create a new method getSettings:

It will run a default (selects all) query from our table with name TABLE_NAME and return a Cursor instance to us. Using this cursor we need to get info about columns and then iterate through rows, while we can do this, and add a setting created from a row to an ArrayList and return this list.

Okay, now we can get created settings, but we should have ability to add settings to database 🙂 New method addSetting will allow us to do that:

It’s very simple – get a setting, convert it to key/value pair and insert it to the writable database.

Also, let’s add other needed methods like edit or remove:

Update method is similar to add, but we also need to set, which row(s) should be updated. It can be reached using where clause:  "id = " + id

Enable method is used for enabling/disabling setting and is a simplified copy of the editSetting method.

Remove function will remove a setting from database, basing on same where clause.

Another database method, that we will need, is checkName. It will check if setting name hasn’t been already taken:

Where “?” in where clause is one by one parameter from whereArgs array .

Okay, so now we have database method for manipulating with settings. Let’s use them where they are needed. First we will connect remove and enable methods to callbacks:

In ResendSettingArrayAdapter our callbacks will look like this:

And also we can now read settings from the database on the main activity:

onResume method is needed for situations, when app was minimized or we returned from another activity (we will use this in the next post, when we will create an “Add Setting” activity) to refresh list of settings.

So, today we learned a bit about using SQLite database in Android app to store our settings and how to manipulate with them. In next post I will show how to create a new Activity for creating/updating setting.

Thanks!

P.S. #1 All source code you can find in my GitHub – https://github.com/dbondarchuk/Notification-Resender

P.S. #2 You can also download app for your bracelet here – https://github.com/dbondarchuk/Notification-Resender/releases

Leave a Reply