Top Banner
ANDROID TRAINING SESSION – 4 -Hussain KMR Behestee
13

Android session 4-behestee

Nov 18, 2014

Download

Education

Android Training session at Jaxara IT
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Android session 4-behestee

ANDROID TRAININGSESSION – 4

-Hussain KMR Behestee

Page 2: Android session 4-behestee

AGENDAS

• Shared Preferences• Storages• SQLite Database– SQLite Open Helper

• Content Providers – SQLite Programming

• Content Providers

Page 3: Android session 4-behestee

Shared Preferences• The SharedPreferences class provides a general framework that allows

you to save and retrieve persistent key-value pairs of primitive data types.• You can save any primitive data: booleans, floats, ints, longs, and strings.

This data will persist across user sessions (even if your application is killed).

• use one of two methods:– getSharedPreferences() - Use this if you need multiple preferences files

identified by name.– getPreferences() - Use this if you need only one preferences.

• Call edit() to get a SharedPreferences.Editor• Add values with methods such as putBoolean() and putString(). • Commit the new values with commit()• To read values - getBoolean() and getString()

Page 4: Android session 4-behestee

Internal Storage• You can save files directly on the device's internal storage. By default, files

saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

• To create and write a private file– Call openFileOutput() with the name of the file and the operating mode. This

returns a FileOutputStream– Write to the file with write()– Close the stream with close()

• To read a file– Call openFileInput() and pass it the name of the file– Read bytes from the file with read()– Then close the stream with close()

• Other useful methods: getFilesDir(), getDir(), deleteFile(), fileList()

Page 5: Android session 4-behestee

External Storage– Checking media availability - Before you do any work with the

external storage, you should always call getExternalStorageState() to check whether the media is available.

– Accessing files on external storage• If you're using API Level 8 or greater, use getExternalFilesDir() to open a File • If you're using API Level 7 or lower, use getExternalStorageDirectory()

– You should then write your data in the following directory:• /Android/data/<package_name>/files/• If the user's device is running API Level 8 or greater and they uninstall your

application, this directory and all its contents will be deleted.

– Hiding your files from the Media Scanner• Include an empty file named .nomedia in your external files directory

Page 6: Android session 4-behestee

External Storage– Saving files that should be shared - These directories lay at the root of

the external storage• Music/ - Media scanner classifies all media found here as user music.• Podcasts/ - Media scanner classifies all media found here as a podcast.• Ringtones/ - Media scanner classifies all media found here as a ringtone.• Alarms/ - Media scanner classifies all media found here as an alarm sound.• Notifications/ - Media scanner classifies all media found here as a notification

sound.• Pictures/ - All photos (excluding those taken with the camera).• Movies/ - All movies (excluding those taken with the camcorder).• Download/ - Miscellaneous downloads.

– Getting Shared Files• In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the

type of public directory you want, such as DIRECTORY_MUSIC,• If you're using API Level 7 or lower, use getExternalStorageDirectory()

Page 7: Android session 4-behestee

SQLite Database – 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.

– to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method. You should use it to Create Table

– To Upgrade your database use onUpgrade() method. You should use it to Alter Table

Page 8: Android session 4-behestee

Android Content Provider

App 1(Dialer)

App 2(Messaging)

App 3(Custom)

App 4(Custom)

Content Provider 1

Data can be shared over different applications

Content Provider Basics

1. There are no common storage area that all Android application can access.2. The only way: to share data across applications: Content Provider3. Content providers store and retrieve data and make it accessible to all

applications.

Content Provider 2

Page 9: Android session 4-behestee

Android Content ProviderContent Provider Basics (Contd.)

Android ships with a number of content providers for common data types: 1. Audio2. Video3. Images4. Personal contact information etc

Content Provider provides the way to share the data between multiple applications.

For example, contact data is used by multiple applications (Dialer, Messaging etc.) and must be stored in Content Provider to have common access.

A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

Content Providerdata

App 1

App 2

Page 10: Android session 4-behestee

Android Content ProviderQuerying Data

Content Provide: URI

1. Each content provider exposes a public URI that uniquely identifies its data set. 2. A content provider that controls multiple data sets (multiple tables) exposes a

separate URI for each one. 3. All URIs for providers begin with the string "content://".

The content: scheme identifies the data as being controlled by a content provider.

URI samples:<standard_prefix>://<authority>/<data_path>/<id>

For example, to retrieve all the bookmarks stored by our web browsers (in Android):content://browser/bookmarks

Similarly, to retrieve all the contacts stored by the Contacts application:content://contacts/people

To retrieve a particular contact, you can specify the URI with a specific ID:content://contacts/people/3

Page 11: Android session 4-behestee

Android Content Provider

Content Provide: URI

So we need three pieces of information to query a content provider:1. The URI that identifies the provider2. The names of the data fields you want to receive3. The data types for those fields

If we are querying a particular record, you also need the ID for that record.

Some more examples:content://media/internal/images URI return the list of all internal images on the device.content://contacts/people/ URI return the list of all contact names on the device.content://contacts/people/45 URI return the single result row, the contact with ID=45.

Page 12: Android session 4-behestee

QUESTION?

?

Page 13: Android session 4-behestee

THANK YOU