0% found this document useful (0 votes)
22 views

Unit 3

Uploaded by

revathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Unit 3

Uploaded by

revathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SharedPreferences SharedPreferences SharedPreferences

• Android provides many ways of storing data of an • Shared – distribute data • Activity preference : getPreferences (int mode)
application. One of this way is called Shared • Preference – important or preferable • developer have to call function getPreferences(int
Preferences API. • Preference File mode)
UNIT III: DATA • Eg:- Username, Password, Firstname, Lastname,
email id, mobile no, DOB, etc..(profile details)
• Preference file is XML file saved in internal memory in
device.
• Use this if you need to use only one shared preference
file for the activity.

PERSISTENCE • Shared Preferences allow you to save and retrieve


data based on key-value pair. (booleans, floats, ints,
longs, and strings).
• You can create a new shared preference file or access
an existing one by calling one of these methods:
• Because this retrieves a default shared preference file
that belongs to the activity.
• Activity preference • Custom Preferences : getSharedPreferences(String
• Datatype variable = value name,int mode)
• Custom preference
• Developer needs to use getSharedPreferences
(String name, int mode) for custom preferences.

SharedPreferences SharedPreferences SharedPreferences


• Save Data In SharedPreferences: • Eg:- To get saved int value just • Below is an example of storing and removing data
• Call the method edit() to get a SharedPreferences. call getInt(“GameScore”,-1) function where from a preference:
Editor; “GameScore” is key and -1 is a default value in case no
• To add SharedPreferences values use the methods value was saved for GameScore.
as putBoolean(), putString(), putint(String Keyname • To remove data call remove(String key)
int value); • To clear all data just call function clear().
• To save the values you must call commit().
• Read or get data from SharedPreferences
• To read SharedPreferences values use the methods as
getBoolean() and getString().

SharedPreferences SharedPreferences File Handling File Handling


• A File object works well for reading or writing large
Internal storage External storage
amounts of data.
• It's always available. • It's not always available,
• The exact location of the where your files can be saved because the user can mount
might vary across devices. • Files saved here are the external storage as USB
storage and in some cases
• To view files on a device, you can log the file location accessible by only your remove it from the device.
provided by methods such as File.getAbsolutePath(), app. • It's world-readable, so files
and then browse the device files with Android • When the user saved here may be read
outside of your control.
Studio's DeviceFileExplorer. uninstalls your app, the • When the user uninstalls
• All Android devices have two file storage areas: system removes all your app, the system
"internal" and "external" storage. removes your app's files
your app's files from from here only if you save
• Most Android devices offered built-in non-volatile internal storage. them in the directory
memory (internal storage), plus a removable storage from getExternalFilesDir().
medium such as a micro SD card (external storage).

File Handling File Handling File Handling File Handling


• Write a file • Write a cache file • Open an existing file
• When saving a file to internal storage, you can • If you instead need to cache some files, you should • To read an existing file, call openFileInput(name),
acquire the appropriate directory as a File by calling use createTempFile(). For example, the following passing the name of the file.
one of two methods: method extracts the file name from a URL and • You can get an array of all your app's file names by
• getFilesDir() creates a file with that name in your app's internal calling fileList().
• Returns a File representing an internal directory for cache directory: • Open a directory
your app. • You can open a directory on the internal file system
• getCacheDir() with the following methods:
• Returns a File representing an internal directory for • getFilesDir()
your app's temporary cache files. • Returns a File representing the directory on the file
• File file = new File(context.getFilesDir(), filename); system that's uniquely associated with your app.
Managing data using Managing data using
File Handling File Handling
SQLite database SQLite database
• getDir(name, mode) • File directory = context.getFilesDir(); • Managing data using SQLite database
• Creates a new directory (or opens an existing • File file = new File(directory, filename); • SQLite is a opensource SQL database that stores
directory) within your app's unique file system data to a text file on a device. Android comes in with
• Delete a file
directory. This new directory appears inside the built in SQLite database implementation.
directory provided by getFilesDir(). • You should always delete files that your app no • You don't need to establish any kind of connections
• getCacheDir() longer need. The most straightforward way to for it like JDBC,ODBC e.t.c
• Returns a File representing the cache directory on delete a file is to call delete() on the File object. • android.database.sqlite package contains the classes
the file system that's uniquely associated with your • myFile.delete(); to manage your own databases.
app. This directory is meant for temporary files, and • Create a database using an SQL helper
it should be cleaned up regularly. The system may To access your database, instantiate your subclass
• In order to create a database you just need to call of SQLiteOpenHelper
delete files there if it runs low on disk space, so this method openOrCreateDatabase with your
make sure you check for the existence of your cache database name and mode as a parameter. FeedReaderDbHelper mDbHelper = newFeedReaderDbHelper(getContext());
files before reading them.

Managing data using Managing data using Managing data using Managing data using
SQLite database SQLite database SQLite database SQLite database
• Put information into a database • Delete information from a database • Update a database • Saving data to a database is ideal for repeating or
• Insert data into the database by passing • To delete rows from a table, you need to provide • When you need to modify a subset of your database structured data, such as contact information.
a ContentValues object to the insert() method: selection criteria that identify the rows to values, use the update() method. • The APIs you'll need to use a database on Android are
• SQLiteDatabase db=mDbHelper.getWritableDatabase(); the delete() method. • Updating the table combines the ContentValues syntax available in the android.database.sqlite package.
• Read information from a database of insert() with the WHERE syntax of delete().
• To read from a database, use the query() method, • SQLiteDatabase db=mDbHelper.getWritableDatabase();
passing it your selection criteria and desired
columns. The method combines elements
of insert() and update()
• SQLiteDatabase db=mDbHelper.getReadableDatabase();

Managing data using Managing data using


Content providers Content providers
SQLite database SQLite database
• Create a database using an SQL helper • Put information into a database
• Once you have defined how your database looks, • Insert data into the database by passing
you should implement methods that create and a ContentValues object to the insert() method:
maintain the database and tables. • The first argument for insert() is simply the table
• Just like files that you save on the device's internal name.
storage, Android stores your database in your app's • The second argument tells the framework what to do
private folder. in the event that the ContentValues is empty
• Your data is secure, because by default this area is
not accessible to other apps or the user.
• The SQLiteOpenHelper class contains a useful set
of APIs for managing your database.

Android in build
Content providers Content providers Content providers
content providers
• Sharing access to your application data with other • A content provider manages access to a central • Accessing a provider • One of the built-in providers in the Android platform
applications repository of data. • When you want to access data in a content provider, is the user dictionary, which stores the spellings of
• Sending data to a widget • A provider is part of an Android application, which you use the ContentResolver object in your non-standard words that the user wants to keep.
• Returning custom search suggestions for your often provides its own UI for working with the data. application's Context to communicate with the
application through the search framework • A content provider presents data to external provider as a client.
using SearchRecentSuggestionsProvider. applications as one or more tables that are similar to • The ContentResolver methods provide the basic
• Synchronizing application data with your server using the tables found in a relational database. "CRUD" (create, retrieve, update, and delete)
an implementation of AbstractThreadedSyncAdapter • A content provider coordinates access to the data functions of persistent storage.
• Loading data in your UI using a CursorLoader storage layer in your application for a number of •A common pattern for accessing
different APIs. a ContentProvider from your UI uses
a CursorLoader to run an asynchronous query in the
background.

You might also like