Click here to load reader

Android session 4-behestee

  • View
    76

  • Download
    1

Embed Size (px)

DESCRIPTION

Android Training session at Jaxara IT

Text of Android session 4-behestee

  • 1. ANDROID TRAINING SESSION 4 -Hussain KMR Behestee

2. AGENDAS Shared Preferences Storages SQLite Database SQLite Open Helper Content Providers SQLite Programming Content Providers 3. 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() 4. 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() 5. 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//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 6. 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() 7. 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 8. 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 Provider 3. Content providers store and retrieve data and make it accessible to all applications. Content Provider 2 9. Android Content Provider Content Provider Basics (Contd.) Android ships with a number of content providers for common data types: 1. Audio 2. Video 3. Images 4. 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 Provider data App 1 App 2 10. Android Content Provider Querying 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: ://// 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 11. Android Content Provider Content Provide: URI So we need three pieces of information to query a content provider: 1. The URI that identifies the provider 2. The names of the data fields you want to receive 3. 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. 12. QUESTION? 13. THANK YOU

Search related