2. Shared Preferences
• Shared Preferences are XML files to store private primitive
data in key-value pairs. Data Types include Booleans, floats,
ints, longs, and strings.
• Data saved in SharedPreferences file is accessible throughout
the application and persists even after the application closes
or across reboots.
3. • In order to use shared preferences, you have to call
a method getSharedPreferences() that returns a
SharedPreference instance pointing to the file that
contains the values of preferences.
How to use?
SharedPreferences sharedpreferences = getSharedPreferences
(“AppPreferences”, Context.MODE_PRIVATE);
• The first parameter is the key and the second
parameter is the MODE.
The data saved using SharedPreferences will be available
even after your application is killed.
The data persists when your application is upgraded!!
4. • You can save something in the sharedpreferences
by using SharedPreferences.Editor class
How to write something in shared
preference?
sharedpreferences.edit().putString("key", "value").apply();
SharedPreferences sharedpreferences =
getSharedPreferences(“AppPreferences”, Context.MODE_PRIVATE);
sharedpreferences.edit().putString("key", "value").commit();
Commit() saves data synchronously where as
apply() saves data in background (asynchronous).
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.apply();
OR
5. Shared Preference Example
1. SharedPreferences sharedpreferences = getSharedPreferences
(“AppPreferences”, Context.MODE_PRIVATE);
1. String value = sharedpreferences.getString("key", "");
2. String value = sharedpreferences.getInt("key", 0);
Second Param is the Value to return if this preference does not exist.
Reading from Shared Preference
6. Using Sqlite for local data storage
SQLite is a opensource SQL database that stores data to a text file on a
device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this
database, you don't need to establish any kind of connections for it like
JDBC,ODBC e.t.c
The main package is android.database.sqlite that contains the classes
to manage your own databases
Database - Helper class
For managing all the operations related to the database , an helper class
has been given and is called SQLiteOpenHelper. It automatically
manages the creation and update of the database.
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
}
}
Desktop sql viewer: https://sqlitebrowser.org/
https://stackoverflow.com/questions/17529766/view-
contents-of-database-file-in-android-studio
8. Inserting a Row in Table
1. Inserting data requires getting writable
instance (getWriteableDatabase()) on
database.
1. ContentValues() is used to define the
column name and its data to be stored.
Here, we are just setting the note value only
ignoring `id` and `timestamp` as these two
will be inserted automatically.
1. Every time the database connection has to
be closed once you are done with
database access. Calling db.close() closes
the connection.
9. Reading a Row in Table
1. Reading data requires getting readable
instance (getReadableDatabase()) on
database.
1. Db.query with params table and column
names with their values return the data set
to cursor.
1. Every time the database connection has to
be closed once you are done with
database access. Calling db.close() closes
the connection.
Cursors are what contain the result set of a query made
against a database in Android
db.close();
10. Database Libraries
1. Room
The Room persistence library provides an abstraction layer over SQLite to allow
for more robust database access while harnessing the full power of SQLite.
https://developer.android.com/training/data-storage/room/
2. Ormlite
ORMLite is lighter version of Object Relational Mapping which provide some
simple functionality for persisting java objects to SQL databases. It is ORM
wrapper over any mobile SQL related DB.
http://ormlite.com/android/examples/
http://ormlite.com/
1. GreenDao
greenDAO is an open source Android ORM making development for SQLite
databases fun again. It relieves developers from dealing with low-level
database requirements while saving development time.
http://greenrobot.org/greendao/
Tutorial: https://datarockets.com/blog/android-architecture-migrate-to-room-from-ormlite/
https://www.tldevtech.com/best-android-orm-libraries-to-use/
11. Permissions
Request permissions at runtime appropriately (in Android 6.0+)
1. Make sure you request permissions at the right time and appropriately educate
and inform your users about the need for permission requests.
Best practices
1. Educate the user before requesting a permission
2. Always check to see if the permission has been granted
3. Ensure users benefit from allowing a permission immediately
4. Help users undo permission denials
Tutorials: https://www.youtube.com/watch?v=iZqDdvhTZj0
https://www.youtube.com/watch?v=C8lUdPVSzDk
https://developer.android.com/training/permissions/requesting
12. Permissions
You should always check for and request permissions at runtime to guard against
runtime errors (SecurityException).
13. Dangerous Permissions
Dangerous permissions cover areas where the
app wants data or resources that involve the
user's private information, or could potentially
affect the user's stored data or the operation of
other apps
Only dangerous permissions require user
agreement.
An app must publicize the permissions it requires by
including <uses-permission> tags in the app manifest.
15. Homework
Create your semester project in Android Studio, integrate any database
library (Room, ormlite etc) for your project and write a simple
read/write/update operation.