Mobile Software Development for Android - I397

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Android og persistens
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
 data/data-storage.html#pref data/data-storage.html#pref 
SQLite in Mobile Apps by Dan Youberg. Overview Many well known applications and Internet browsers use SQLite due to its very small size (~250 Kb). Also.
CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
Data Persistence in Android
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Data Storage: Part 1 (Preferences)
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Database Rung-Hung Gau Department of Computer Science and Engineering
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
SQLite (part deux) 1 CS440. Traditional Model View Controller (MVC) CS440 2.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
SQlite. 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.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
CHAPTER 9 File Storage Shared Preferences SQLite.
Android Storage MAY 2013 Hu.Cai. NAME OF PRESENTATION [CHANGE IN SLIDE MASTER] MONTH, YEAR [CHANGE IN SLIDE MASTER] Outline 1.Storage In General 2.SharedPreferences.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
CS371m - Mobile Computing Persistence - SQLite. 2 In case you have not taken 347: Data Management or worked with databases as part of a job, internship,
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Sqlite primer.
Mobile Applications (Android Programming)
SQL – Python and Databases
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
SQLite in Android Landon Cox March 2, 2017.
Mobile Application Development BSCS-7 Lecture # 18, 19
Android Application SQLite 1.
Reactive Android Development
Android Database using SQLite
Android 4: Saving Data Kirk Scott.
Mobile Application Development Chapter 5 [Persistent Data in Android]
Android 18: Saving Data Kirk Scott.
Mobile Computing With Android ACST Android Database Storage Part 2
Android Programming Lecture 7
CMPE419 Mobile Application Development
CS371m - Mobile Computing Persistence - SQLite.
CIS 136 Building Mobile Apps
CMPE419 Mobile Application Development
Mobile Computing With Android ACST 4550 Android Database Storage
Android Developer Fundamentals V2
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
Preference Activity class
Presentation transcript:

Mobile Software Development for Android - I397 IT College, Andres Käver, 2015-2016 Email: akaver@itcollege.ee Web: http://enos.itcollege.ee/~akaver/2015-2016/Distance/Android Skype: akaver

Android - Data persistence Shared preferences Internal storage External storage SQLite Database Network connection

Android - Data persistence Saving data inbetween same Activity instances Data forwarding actually! onSaveInstanceState onRestoreInstanceState

Android – Shared preferences SharedPreferences – save and retrieve key-value pairs Any primitive data Booleans Floats Ints Longs Strings

Android – Shared preferences To get a SharedPreferences object getSharedPreferences(name, mode) – if you need multiple pref files, identified by name (first parameter) getPreferences(mode) – single pref file for activity, no name specified Mode MODE_PRIVATE (0) - default MODE_WORLD_READABLE MODE_WORLD_WRITEABLE

Android – Shared preferences To WRITE values Call edit() to get SharedPreferences.Editor Add values with putString(), putBoolean, … Commit values with commit() public class Calc extends Activity {     public static final String PREFS_NAME = "MyPrefsFile";     @Override     protected void onStop(){        super.onStop();       // We need an Editor object to make preference changes.       // All objects are from android.context.Context       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       SharedPreferences.Editor editor = settings.edit();       editor.putBoolean("silentMode", mSilentMode);       // Commit the edits!       editor.commit();     } }

Android – Shared preferences To READ values Use methods such as getString, getBoolean public class Calc extends Activity {     public static final String PREFS_NAME = "MyPrefsFile";     @Override     protected void onCreate(Bundle state){        super.onCreate(state);        . . .        // Restore preferences        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);        boolean silent = settings.getBoolean("silentMode", false);        setSilent(silent);     } }

Android – Internal storage Private to application (default) No access for user or other apps When app gets uninstalled – files are deleted with it

Android – Internal storage Create and Write private file Call openFileOutput(), with filename and operating mode Returns FileOutputStream Write to the file with write() Close the file with close() Modes MODE_PRIVATE – create (or replace) MODE_APPEND MODE_WORLD_READABLE MODE_WORLD_WRITEABLE String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();

Android – Internal storage Read from private file Call openFileInput() with filename Returns FileInputStream Get bytes with read() Close stream with close()

Android – Internal storage Save static file during compile time Place in /res/raw Open with OpenRawResource(), passing R.raw.<filename> Returns inputStream File is read-only!!!

Android – Internal storage Caching data Do not need data forever getCahceDir() When space is low, Android will delete cache Stay within reasonable space limits (1mb?) Deleted with uninstall Manage cache files yourself

Android – Internal storage Other useful methods getFilesDir() - Gets the absolute path to the filesystem directory where your internal files are saved. getDir() - Creates (or opens an existing) directory within your internal storage space. deleteFile() - Deletes a file saved on the internal storage. fileList() - Returns an array of files currently saved by your application.

Android – External storage All Android devices support shared “external storage” Can be removable storage media (sd-card) Or internal, non-removable storage Files are world-readable

Android – External storage Getting access – manifest READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE <manifest ...>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     ... </manifest>

Android – External storage Check availability Use getExternalStorageState() Media might be mounted to a computer, missing, read-only, or in some other state. /* Checks if external storage is available for read and write */ public boolean isExternalStorageWritable() {     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state)) {         return true;     }     return false; } /* Checks if external storage is available to at least read */ public boolean isExternalStorageReadable() {     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state) ||         Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {         return true;     }     return false; }

Android – External storage Files acquired through your app should be saved to a "public" location where other apps can access them and the user can easily copy them from the device Use one of the shared public directories (Music/, Pictures/, and Ringtones/) Use getExternalStoragePublicDirectory() DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES public File getAlbumStorageDir(String albumName) {     // Get the directory for the user's public pictures directory.     File file = new File(Environment.getExternalStoragePublicDirectory(             Environment.DIRECTORY_PICTURES), albumName);     if (!file.mkdirs()) {         Log.e(LOG_TAG, "Directory not created");     }     return file; }

Android – External storage - private Private files (textures, sounds for app, etc) (actually semi-private) Use a private storage directory on the external storage Use getExternalFilesDir() Takes Type, use null when no type From 4.4 onwards does not require permissions Files are hidden from Media Scanner (but not from other apps with permissions)

Android – Database (SQLite) Full support for SQLite Any database will be accessible by name in any class in app Private to your app http://sqlite.org/docs.html

Android - SQLite Create DB Use SQLiteOpenHelper public class DictionaryOpenHelper extends SQLiteOpenHelper {     private static final int DATABASE_VERSION = 2;     private static final String DICTIONARY_TABLE_NAME = "dictionary";     private static final String DICTIONARY_TABLE_CREATE =                 "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +                 KEY_WORD + " TEXT, " +                 KEY_DEFINITION + " TEXT);";     DictionaryOpenHelper(Context context) {         super(context, DATABASE_NAME, null, DATABASE_VERSION);     }     @Override     public void onCreate(SQLiteDatabase db) {         db.execSQL(DICTIONARY_TABLE_CREATE);     } } Create DB Use SQLiteOpenHelper Override onCreate(SQLiteDatabase db) execute a SQLite commands to create tables in the database The database tables should use the identifier _id for the primary key of the table. Several Android functions rely on this standard.

Android - SQLite Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

Android - SQLite Get an instance of your SQLiteOpenHelper implementation using the constructor you've defined To write to db - getWritableDatabase() Read from the db – getReadableDatabase() Both return a SQLiteDatabase object, providing methods for SQLite operations.

Android – SQLite – Data types NULL INTEGER REAL TEXT - database encoding (UTF-8, UTF-16BE or UTF-16LE) BLOB Everything else is mapped into one of these types Boolean – int 0/1 DateTime – integer, unix timestap or text …. http://www.sqlite.org/datatype3.html

Android – SQLite – Data types Date and time functions date(timestring, modifier, modifier, ...) time(timestring, modifier, modifier, ...) datetime(timestring, modifier, modifier, ...) julianday(timestring, modifier, modifier, ...) strftime(format, timestring, modifier, modifier, ...) http://www.sqlite.org/lang_datefunc.html

Android - SQLite SQLiteDatabase provides Insert Update Delete execSQL Queries can be created via rawQuery - directly accepts an SQL select statement as input query - provides a structured interface for specifying the SQL query SQLiteQueryBuilder -  convenience class that helps to build SQL queries

Android - SQLite rawQuery Cursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id = ?", new String[] { id }); rawQuery

Android - SQLite query Cursor cursor = database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);

Android - SQLite query If a condition is not required you can pass null, e.g. for the group by clause. The "whereClause" is specified without the word "where", for example a "where" statement might look like: "_id=19 and summary=?" If you specify placeholder values in the where clause via ?, you pass them as the selectionArgs parameter to the query

Android – SQLite - Cursor A query returns a Cursor object. A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory. To get the number of elements of the resulting query use the getCount() method. To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method allows to check if the end of the query result has been reached. Cursor provides typed get*() methods, e.g. getLong(columnIndex), getString(columnIndex) to access the column data for the current position of the result. The "columnIndex" is the number of the column you are accessing. Cursor also provides the getColumnIndexOrThrow(String) method which allows to get the column index for a column name of the table. A Cursor needs to be closed with the close() method call.

Android – SQLite - Insert public long insert (String table, String nullColumnHack, ContentValues values) table - the table to insert the row into nullColumnHack - optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty. values - this map contains the initial column values for the row. The keys should be the column names and the values the column values

Android – SQLite - Insert public long insert (String table, String nullColumnHack, ContentValues values) void saveToDb(){ ContentValues insertValues = new ContentValues(); insertValues.put("Description", "Electricity"); insertValues.put("Amount", 500); insertValues.put("Trans", 1); insertValues.put("EntryDate", "04/06/2011"); db.insert("CashData", null, insertValues); }

Android – SQLite - Update public int update (String table, ContentValues values, String whereClause, String[] whereArgs) table - the table to update in values - a map from column names to new column values. null is a valid value that will be translated to NULL. whereClause - the optional WHERE clause to apply when updating. Passing null will update all rows. whereArgs - You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.

Android – SQLite - Update public int update (String table, ContentValues values, String whereClause, String[] whereArgs) void updateDb(){ ContentValues args = new ContentValues(); args.put(columnName, newValue); db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null); }

Android – SQLite - Delete public int delete (String table, String whereClause, String[] whereArgs) table - the table to delete from whereClause - the optional WHERE clause to apply when deleting. Passing null will delete all rows. whereArgs - You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.

Android – SQLite - DEMO