Android has a significant number of ways for you to save data on your smartphone, from private data storage for your application, called shared preferences, to internal storage on your smartphone device’s memory chips, to external storage via your smartphone device’s external storage (SD card or Micro-SD), to network connection (Network Accessed Storage) via your own network server, to an entire DBMS (database management system) via open source SQLite private databases.
Shared Preferences :
Shared preferences are persistent data pairs that remain in
memory even if your application is killed (or crashes), and thus this data
remains persistent across multiple user sessions. The primary use of shared
preferences is to store user preferences for a given user’s Android
applications and this is a main reason why they persist in memory between
application runs.
To set your application’s shared preferences Android
provides us with the SharedPreferences class. This class can be used to store
any primitive data types, including Booleans (1/0, on/off, true/false, or
visible/hidden), floats, integers, strings, and longs. Note that the data
created with this class will remain persistent across user sessions with your
application even if your application is killed (the process is terminated, or
crashes).
There are two methods in the SharedPreferences class that
are used to access the preferences; if you have a single preference file use getPreferences
and if you have more than one preference file, you can name each and use getSharedPreferences(name)
and access them by name.
public static final String PREFS NAME =
"PreferenceFile";
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity main);
SharedPreferences settings = getSharedPreferences(PREFS
NAME, 0);
String screenName =
settings.getString("screenName", "Android Fan");
// do something with the screen name.
}
We can set the screen name with the following:
SharedPreferences settings = getSharedPreferences(PREFS
NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("screenName", screenName);
editor.commit();
Internal Memory :
Accessing internal memory storage on Android is done a bit
differently, as that memory is unique to your application and cannot be
directly accessed by the user or by other applications. When the application is
uninstalled these files are deleted from memory. To access files in memory use the
openFileOutput() with the name of the file and the operation needed, which will
return a FileOutputStream object that you can use the read(), write(), and close()
methods to manipulate the data into and out of the file.
String FILENAME = "hello file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE
PRIVATE);
fos.write(string.getBytes());
fos.close();
External Memory :
The method that is used for accessing external memory on an
Android device is getExternalStorageState. It checks to see whether the media
(usually an SD card or internal micro SD) is in place (inserted in the slot for
an SD card) and available for usage. Note that files written to external
removable storage media also can be accessed outside of Android and its applications
by PCs or other computing devices that can read the SD card format. This means there
is no security in place on files that are written to external removable storage
devices.
SQLite:
The most common way to store data for your application, and
the most organized and sharable, is to create and utilize a SQLite database.
This is how Android stores and accesses its own data for users who utilize its
internal applications such as the Contacts list or Database.
Any private
database you create for your application will be accessible to all parts of
your application, but not to other parts of other developer’s applications
unless you give permission for them to access it. To write and read from a
custom database structure, you would utilize the getWritableDatabase and getReadableDatabase
methods, which return a SQLiteDatabase object that represents the database structure
and provides methods for performing SQLite database operations.
To perform
SQLite database queries on your new SQLite database you would use the SQLiteDatabase
query() methods, which accept all common data query parameters such as the table
to query and the groupings, columns, rows, selections, projection, and similar
concepts that are mainstream in database programming.
No comments:
Post a Comment