selected values etc. Internal storage for large private data sets Example: private pictures, videos etc. External storage for large public data sets Example: pictures, videos etc. SQLite databases for structured storage Example: customer database, statistics, products etc.
types Example: int, long, double etc. + String • Private to your applications • Persists across user sessions • Cleared when user uninstalls application • User can manually clear going under settings > apps > clear data
getSharedPreferences(String name, int mode) multiple preferences files by name getPreferences(int mode) single preference for an Activity mode: 0 - Context.MODE_PRIVATE 4 - Context.MODE_MULTI_PROCESS
text, images, videos, raw binary etc. • Private to your applications • Persists across user sessions • Cleared when user uninstalls application • User can manually clear going under settings > apps > clear data
text, images, videos, raw binary etc. • Needs permission “WRITE_EXTERNAL_STORAGE” • Can be used for public files • Persists across user sessions • If data is public, then it is not deleted after app uninstall File object Defines reference to resource Example: File file = new File(path, filename);
new byte[is.available()]; is.read(data); File file = new File(path, "DemoPicture.jpg"); OutputStream os = new FileOutputStream(file); os.write(data); // close is and os Read File file = new File(path, "DemoPicture.jpg");
accessible outside the application. • Needs to extend SQLiteOpenHelper • To write call getWritableDatabase() • To read call getReadableDatabase(), • Execute SQL using query() • SQLite query will return a Cursor pointing to all the rows
mode SQLiteDatabase db = DbHelper.getWritableDatabase(); // Create a new map of values, where column names are the keys ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id); values.put(FeedEntry.COLUMN_NAME_TITLE, title); values.put(FeedEntry.COLUMN_NAME_CONTENT, content); // Insert the new row, returning the primary key of the new row long newRowId = db.insert(FeedEntry.TABLE_NAME, FeedEntry.COLUMN_NAME_NULLABLE, values);
projection that specifies which columns from the database // you will actually use after this query. String[] projection = {FeedEntry._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_UPDATED, ...}; Cursor c = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The columns to return null, // The columns for the WHERE clause null, // The values for the WHERE clause null, // don't group the rows null, // don't filter by row groups null // The sort order );
• Register/unregister for periodic location updates • Register/unregister for a given Intent to be fired if the device comes within a given proximity Example: LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; LocationManager locationManager = (LocationManager) this. getSystemService(Context.LOCATION_SERVICE); // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager. NETWORK_PROVIDER, MINIMUM_TIME_MS, MINIMUM_DISTANCE_M, locationListener);