You are on page 1of 40

Data

 Storage  Methods  
in  Android  
Data  Storage  
•  Applica4ons  o5en  need  to  use  some  type  of  data  storage  for  
persistent  data.  
•  The  Android  framework  offers  different  data  storage  op4ons:  
–  Shared  preferences:  
•  Useful  for  storing  primi4ve  data  in  key-­‐value  pairs  
•  Lightweight  usage,  such  as  saving  applica4on  seEngs,  UI  state  
–  Flat  file  based  storage:  
•  Storing  private  data  in  device  memory  
•  Storing  public  data  on  external  storage,  e.g.,  SD  card  
•  Uses  standard  Java  file  storage  access  methods:  InputFileStream  and  
OutputFileStream  
–  SQLite  database:  
•  Provides  a  means  of  storing  structured  data  in  a  private  database  
•  You  can  also  store  your  data  on  a  network  server  and  access  it  
using  the  network  

COMP 355 (Muppala) Data Storage 2


Shared  Preferences  

COMP 355 (Muppala) Data Storage 3


Shared  Preferences  
•  This  class  provides  a  general  framework  that  allows  saving  
and  retrieving  of  persistent  key-­‐value  pairs  of  primi4ve  data  
•  Typical  usage  of  SharedPreferences  is  for  saving  applica4on  
seEngs  such  as  user  seEngs,  theme,  login  informa4on  such  
as  username  and  password,  auto  login  flag,  remember-­‐user  
flag  etc.  
•  The  shared  preferences  informa4on  is  stored  in  an  XML  file  
on  the  device  
–  Typically  in  /data/data/<Your  applica4on’s  package  name>/
shared_prefs  
•  SharedPreferences  can  be  associated  with  the  en4re  
applica4on,  or  to  a  specific  ac4vity  

COMP 355 (Muppala) Data Storage 4


Shared  Preferences  
•  Use  the  getSharedPreferences()  method  to  get  access  to  the  shared  
preferences  
•  Example:  
SharedPreferences  prefs  =  this.getSharedPreferences(“myPrefs”,  
MODE_PRIVATE);  
•  If  the  preferences  XML  file  exists,  it  is  opened,  otherwise  it  is  
created  
•  To  control  access  permissions  to  the  file:  
–  MODE_PRIVATE:  private  only  to  the  applica4on  
–  MODE_WORLD_READABLE:  all  applica4ons  can  read  the  XML  file  
–  MODE_WORLD_WRITEABLE:  all  applica4ons  can  write  the  XML  file  
•  If  you  are  using  SharedPreferences  for  a  specific  ac4vity,  then  use  
getPreferences()  method  
–  No  need  to  specify  the  name  of  the  preferences  file  

COMP 355 (Muppala) Data Storage 5


Shared  Preferences  
•  To  add  shared  preferences,  first  an  Editor  object  is  needed:  
Editor  prefsEditor  =  prefs.edit();  
•  Then,  use  the  put()  method  to  add  the  key-­‐value  pairs:  
prefsEditor.putString(“username”,  “user1”);  
prefsEditor.putString(“password”,  “pass1234”);  
prefsEditor.putInt(“4mes-­‐login”,1);  
prefsEditor.commit();  
•  To  retrieve  shared  preferences  data:  
String  username  =  prefsEditor.getString(“username”,””);  
String  password  =  prefsEditor.getString(“password”,””);  
 

COMP 355 (Muppala) Data Storage 6


User  Preferences  and  PreferenceAc4vity  

•  SeEng  user  preferences  for  an  applica4on  is  supported  in  


Android  through  the  PreferenceAc4vity  class  
–  Provides  an  ac4vity  framework  to  create  user  preferences  
–  Preferences  are  automa4cally  persisted  using  shared  preferences  
•  To  add  preferences  to  your  applica4on:  
–  Create  a  preferences.xml  file  in  the  /res/xml  directory:  
–  Create  a  new  ac4vity  extending  PreferenceAc4vity  and  reading  in  the  
default  preferences  from  the  xml  file  above  
–  Add  the  ac4vity  to  your  AndroidManifest.xml  file  
–  Invoke  your  preference  ac4vity  from  another  ac4vity  

COMP 355 (Muppala) Data Storage 7


Flat  File  Storage  

COMP 355 (Muppala) Data Storage 8


Flat  File  Storage
•  Files  are  used  to  store  large  amount  of  data  
•  Generally  not  recommended  to  use  files  
•  Use  I/O  interfaces  provided  by  java.io.*  libraries  to  read/write  
files.  
•  Only  local  files  can  be  accessed.  
–  Advantage:  can  store  large  amount  of  data.  
–  Disadvantage:  Upda4ng  files  or  changing  their  format  may  lead  to  
massive  coding  work.  

COMP 355 (Muppala) Data Storage 9


File  Opera4on  (Read)  
•  Read  file
–  Use  Context.openFileInput(String  name)  to  open  a  private  input  file  
stream  related  to  a  program  
–  Throw  FileNotFoundExcep4on  when  the  file  does  not  exist.  
 
FileInputStream  in  =  this.openFileInput(“rt.txt");//Open  file  “rt.txt"  
……  
in.close();//Close  input  stream

COMP 355 (Muppala) Data Storage 10


File  Opera4on  (Write)  
•  Write  File
–  Use  Context.openFileOutput(String  name,int  mode)  to  open  a  private  
output  file  stream  related  to  a  program  
–  The  file  will  be  created  if  it  does  not  exist.
–  Output  stream  can  be  opened  in  append  mode,  which  means  new  
data  will  be  appended  to  end  of  the  file.  
String  mystring  =  “Hello,  World!”;  
 
FileOutputStream  ouoile  =  this.openFileOutput(“myfile.txt",MODE_APPEND);  
//Open  and  write  in  “myfile.txt”,  using  append  mode.    
Ouoile.write(mystring.getBytes());  
ouoile.close();//Close  output  stream Also possible to use:
MODE_PRIVATE,
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
COMP 355 (Muppala) Data Storage 11
Read  Sta4c  Files
•  Sta4c  files  that  are  included  in  the  applica4on  are  created  in  
the  /res/raw  directory  
•  Read-­‐only  sta4c  files  
•  Use  Resources.openRawResource(R.raw.mydatafile)  to  open  
sta4c  files.  
InputStream  myFile  =  getResources().openRawResource(R.raw.myfilename);  
…    //Get  context  resource
myFile.close();  //Close  input  stream

COMP 355 (Muppala) Data Storage 12


External  Storage  
•  Every  Android-­‐compa4ble  device  supports  a  shared  "external  
storage"  that  you  can  use  to  save  files  
–  Removable  storage  media  (such  as  an  SD  card)    
–  Internal  (non-­‐removable)  storage  
•  Files  saved  to  the  external  storage  are  world-­‐readable  and  
can  be  modified  by  the  user  when  they  enable  USB  mass  
storage  to  transfer  files  on  a  computer  

COMP 355 (Muppala) Data Storage 13


External  Storage
•  Must  check  whether  external  storage  is  available  first  by  calling  
getExternalStorageState()  
–  Also  check  whether  it  allows  read/write  before  reading/wri4ng  on  it  
•  Example:  
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
             File  sdCardDir  =  Environment.getExternalFilesDir(null);//Get  SD  Card  directory  
             File  outFile  =  new  File(sdCardDir,  “example.txt”);  
             FileOutputStream  outStream  =  new  FileOutputStream(outFile);  
             outStream.write(”Hello,  world!".getBytes());  
             outStream.close();  
}  
•  These  files  are  private  to  the  applica4on  and  will  be  removed  when  
the  applica4on  is  uninstalled  
•  getExternalFilesDir()  takes  a  parameter  such  as  DIRECTORY_MUSIC,  
DIRECTORY_RINGTONES  etc.,  to  open  specific  type  of  
subdirectories  

COMP 355 (Muppala) Data Storage 14


External  Storage  
•  For  public  shared  directories,  use  
getExternalStoragePublicDirectory()  
•  For  cache  files,  use  getExternalCacheDir()  
•  All  these  are  applicable  for  API  level  8  or  above  
•  For  API  level  7  or  below,  use  the  method:  
–  getExternalStorageDirectory()  
–  private  files  stored  in  /Android/data/<package_name>/files/  
–  Cache  files  stored  in  /Android/data/<package_name>/cache/  

COMP 355 (Muppala) Data Storage 15


SQLite  Databases  

COMP 355 (Muppala) Data Storage 16


SQLite  Database
•  Android  applica4ons  can  have  applica4on  databases  powered  by  
SQLite
–  Lightweight  and  file-­‐based,  ideal  for  mobile  devices  
–  Databases  are  private  for  the  applica4on  that  creates  them  
–  Databases  should  not  be  used  to  store  files
•  SQLite  is  a  light  weight  database
–  Atomic  
–  Stable  
–  Independent  
–  Enduring  
–  Only  several  kilobytes
–  Only  partly  support  some  SQL  commands  such  as  ALTER,  TABLE.  
•  SQLite  is  included  as  part  of  Android’s  so5ware  stack  
•  More  info  about  SQLite  at  hup://www.sqlite.org

COMP 355 (Muppala) Data Storage 17


SQLite  Databases  
•  Steps  for  using  SQLite  databases:  
1.  Create  a  database  
2.  Open  the  database  
3.  Create  a  table  
4.  Create  and  insert  interface  for  datasets  
5.  Create  a  query  interface  for  datasets  
6.  Close  the  database  
•  Good  prac4ce  to  create  a  Database  Adapter  class  to  simplify  
your  database  interac4ons  
•  We  will  use  the  SQLite  database  defined  in  the  notebook  
tutorial  as  an  example  

COMP 355 (Muppala) Data Storage 18


SQLite  Example:  Notebook  Tutorial  
public  class  NotesDbAdapter  {  
 
       public  sta3c  final  String  KEY_TITLE  =  "+tle";  
       public  sta3c  final  String  KEY_BODY  =  "body";  
       public  sta3c  final  String  KEY_ROWID  =  "_id";  
 
       private  sta3c  final  String  TAG  =  "NotesDbAdapter";  
       private  DatabaseHelper  mDbHelper;  
       private  SQLiteDatabase  mDb;  
 
       /**  
         *  Database  crea4on  sql  statement  
         */  
       private  sta3c  final  String  DATABASE_CREATE  =  
               "create  table  notes  (_id  integer  primary  key  autoincrement,  "  
               +  "4tle  text  not  null,  body  text  not  null);";  
 
       private  sta3c  final  String  DATABASE_NAME  =  "data";  
       private  sta3c  final  String  DATABASE_TABLE  =  "notes";  
       private  sta3c  final  int  DATABASE_VERSION  =  2;  
 
       private  final  Context  mCtx;  

COMP 355 (Muppala) Data Storage 19


SQLiteOpenHelper  Class  
•  Abstract  class  for  implemen4ng  a  best  prac4ce  pauern  for  crea4ng,  
opening  and  upgrading  databases  
•  To  create  a  SQLite  database,  the  recommended  approach  is  to  create  a  
subclass  of  SQLiteOpenHelper  class  
•  Then  override  its  onCreate()  method  
–  Then  execute  a  SQLite  command  to    create  tables  in  the  database  
•  Use  the  onUpgrade()  method  to  handle  upgrade  of  the  database  
–  A  simple  way  would  be  to  drop  an  exis4ng  table  and  replace  with  a  new  defeni4on  
–  Beuer  to  migrate  exis4ng  data  into  a  new  table  
•  Then  use  an  instance  of  the  helper  class  to  manage  opening  or  upgrading  
the  database  
–  If  the  database  doesn’t  exist,  the  helper  will  create  one  by  calling  its  onCreate()  
handler  
–  If  the  database  version  has  changed,  it  will  upgrade  by  calling  the  onUpgrade()  
handler  

COMP 355 (Muppala) Data Storage 20


SQLite  Example:  Notebook  Tutorial  
 private  sta3c  class  DatabaseHelper  extends  SQLiteOpenHelper  {  
 
               DatabaseHelper(Context  context)  {  
                       super(context,  DATABASE_NAME,  null,  DATABASE_VERSION);  
               }  
 
               @Override  
               public  void  onCreate(SQLiteDatabase  db)  {  
 
                       db.execSQL(DATABASE_CREATE);  
               }  
 
               @Override  
               public  void  onUpgrade(SQLiteDatabase  db,  int  oldVersion,  int  newVersion)  {  
                       Log.w(TAG,  "Upgrading  database  from  version  "  +  oldVersion  +  "  to  "  
                                       +  newVersion  +  ",  which  will  destroy  all  old  data");  
                       db.execSQL("DROP  TABLE  IF  EXISTS  notes");  
                       onCreate(db);  
               }  
       }  

COMP 355 (Muppala) Data Storage 21


SQLite  Example:  Notebook  Tutorial  
 public  NotesDbAdapter(Context  ctx)  {  
               this.mCtx  =  ctx;  
       }  
 
 public  NotesDbAdapter  open()  throws  SQLExcep3on  {  
               mDbHelper  =  new  DatabaseHelper(mCtx);  
               mDb  =  mDbHelper.getWritableDatabase();  
               return  this;  
       }  
 
       public  void  close()  {  
               mDbHelper.close();  
       }  

COMP 355 (Muppala) Data Storage 22


SQLite  Databases  
•  ContentValues()  objects  used  to  hold  rows  to  be  inserted  into  the  database  
•  Example:  
 public  long  createNote(String  3tle,  String  body)  {  
               ContentValues  ini4alValues  =  new  ContentValues();  
               ini4alValues.put(KEY_TITLE,  Mtle);  
               ini4alValues.put(KEY_BODY,  body);  
 
               return  mDb.insert(DATABASE_TABLE,  null,  ini+alValues);  
       }  
 
 public  boolean  deleteNote(long  rowId)  {  
 
           return  mDb.delete(DATABASE_TABLE,  KEY_ROWID  +  "="  +  rowId,  null)  >  0;  
 }  
 public  boolean  updateNote(long  rowId,  String  3tle,  String  body)  {  
               ContentValues  args  =  new  ContentValues();  
               args.put(KEY_TITLE,  Mtle);  
               args.put(KEY_BODY,  body);  
 
               return  mDb.update(DATABASE_TABLE,  args,  KEY_ROWID  +  "="  +  rowId,  null)  >  0;  
       }  

COMP 355 (Muppala) Data Storage 23


SQLite  Databases  
•  Database  queries  are  returned  as  Cursor  objects  
–  Pointers  to  the  resul4ng  sets  within  the  underlying  data  
•  Cursor  class  provides  several  methods:  
–  moveToFirst,  moveToNext,  moveToPrevious,  moveToPosi4on  used  to  
move  to  a  row  
–  getCount  to  get  the  number  of  rows  in  the  cursor  
–  getPosi4on  to  get  the  current  row  posi4on  
–  getColumnName,  getColumnNames,  getColumnIndexorThrow  to  get  
info  on  columns  
–  startManagingCursor  and  stopManagingCursor  methods  used  to  
integrate  cursor  life4me  into  the  ac4vity’s  life4me  

COMP 355 (Muppala) Data Storage 24


SQLite  Example:  Notebook  Tutorial  
 public  Cursor  fetchAllNotes()  {  
 
               return  mDb.query(DATABASE_TABLE,  new  String[]  {KEY_ROWID,  KEY_TITLE,  
                               KEY_BODY},  null,  null,  null,  null,  null);  
       }  
 public  Cursor  fetchNote(long  rowId)  throws  SQLExcep3on  {  
 
               Cursor  mCursor  =  
 
                       mDb.query(true,  DATABASE_TABLE,  new  String[]  {KEY_ROWID,  
                                       KEY_TITLE,  KEY_BODY},  KEY_ROWID  +  "="  +  rowId,  null,  
                                       null,  null,  null,  null);  
               if  (mCursor  !=  null)  {  
                       mCursor.moveToFirst();  
               }  
               return  mCursor;  
 
       }  

COMP 355 (Muppala) Data Storage 25


SQLite  Example:  Notebook  Tutorial  
•  Within  the  main  ac4vity,  cursors  returned  by  the  Dbadapter  are  used  as  
follows:  
       private  void  fillData()  {  
               Cursor  notesCursor  =  mDbHelper.fetchAllNotes();  
               startManagingCursor(notesCursor);  
 
               //  Create  an  array  to  specify  the  fields  we  want  to  display  in  the  list  (only  TITLE)  
               String[]  from  =  new  String[]{NotesDbAdapter.KEY_TITLE};  
 
               //  and  an  array  of  the  fields  we  want  to  bind  those  fields  to  (in  this  case  just  text1)  
               int[]  to  =  new  int[]{R.id.text1};  
 
               //  Now  create  a  simple  cursor  adapter  and  set  it  to  display  
               SimpleCursorAdapter  notes  =    
                       new  SimpleCursorAdapter(this,  R.layout.notes_row,  notesCursor,  from,  to);  
               setListAdapter(notes);  
       }  
 
COMP 355 (Muppala) Data Storage 26
Content  Providers  

COMP 355 (Muppala) Data Storage 27


Content  Providers  
•  Store  and  retrieve  data  and  make  it  available  to  all  applica4ons  
–  Only  way  to  share  data  across  applica4ons  
•  Standard  content  providers  part  of  Android:  
–  Common  data  types  (audio,  video,  images,  personal  contact  informa4on)  
•  Applica4ons  can  create  their  own  content  providers  to  make  their  
data  public  
–  Alterna4vely  add  the  data  to  an  exis4ng  provider  
•  Implement  a  common  interface  for  querying  the  provider,  adding,  
altering  and  dele4ng  data  
•  Actual  storage  of  data  is  up  to  the  designer  
•  Provides  a  clean  separa4on  between  the  applica4on  layer  and  data  
layer  

COMP 355 (Muppala) Data Storage 28


Accessing  Content  
•  Applica4ons  access  the  content  through  a  ContentResolver  
instance  
–  ContentResolver  allows  querying,  inser4ng,  dele4ng  and  upda4ng  
data  from  the  content  provider  
ContentResolver  cr  =  getContentResolver();  
 
cr.query(People.CONTENT_URI,  null,  null,  null,  null);  //querying  contacts  
 
ContentValues  newvalues  =  new  ContentValues();  
cr.insert(People.CONTENT_URI,  newvalues);  
 
cr.delete(People.CONTENT_URI,  null,  null);  //delete  all  contacts  

COMP 355 (Muppala) Data Storage 29


Content  Providers  
•  Content  providers  expose  their  data  as  a  simple  table  on  a  
database  model  
–  Each  row  is  a  record  and  each  column  is  data  of  a  par4cular  type  and  
meaning  
•  Queries  return  cursor  objects  
•  Each  content  provider  exposes  a  public  URI  that  uniquely  
iden4fies  its  data  set  
–  Separate  URI  for  each  data  set  under  the  control  of  the  provider  
–  URIs  start  with  content://…  
–  Typical  format:  
Content://<package  name>.provider.<custom  provider  name>/<DataPath>  

COMP 355 (Muppala) Data Storage 30


Content  Providers:  Query  
•  You  need  three  pieces  of  informa4on  to  query  a  content  provider:  
–  The  URI  that  iden4fies  the  provider  
–  The  names  of  the  data  fields  you  want  to  receive  
–  The  data  types  for  those  fields  
•  If  you're  querying  a  par4cular  record,  you  also  need  the  ID  for  that  record  
•  Example:  
import  android.provider.Contacts.People;  
import  android.content.ContentUris;  
import  android.net.Uri;  
import  android.database.Cursor;  
 
//  Use  the  ContentUris  method  to  produce  the  base  URI  for  the  contact  with  _ID  ==  23.  
Uri  myPerson  =  ContentUris.withAppendedId(People.CONTENT_URI,  23);  
 
//  Alterna4vely,  use  the  Uri  method  to  produce  the  base  URI.  
//  It  takes  a  string  rather  than  an  integer.  
Uri  myPerson  =  Uri.withAppendedPath(People.CONTENT_URI,  "23");  
 
//  Then  query  for  this  specific  record:  
Cursor  cur  =  managedQuery(myPerson,  null,  null,  null,  null);  

COMP 355 (Muppala) Data Storage 31


Content  Providers:  Query  
import  android.provider.Contacts.People;  
import  android.database.Cursor;  
 
//  Form  an  array  specifying  which  columns  to  return.    
String[]  projec4on  =  new  String[]  {  
                                                         People._ID,  
                                                         People._COUNT,  
                                                         People.NAME,  
                                                         People.NUMBER  
                                                   };  
 
//  Get  the  base  URI  for  the  People  table  in  the  Contacts  content  provider.  
Uri  contacts  =    People.CONTENT_URI;  
 
//  Make  the  query.    
Cursor  managedCursor  =  managedQuery(contacts,  
                                                 projec4on,  //  Which  columns  to  return    
                                                 null,              //  Which  rows  to  return  (all  rows)  
                                                 null,              //  Selec4on  arguments  (none)  
                                                 //  Put  the  results  in  ascending  order  by  name  
                                                 People.NAME  +  "  ASC");  

COMP 355 (Muppala) Data Storage 32


Content  Providers:  Query  
•  Retrieving  the  data:  
import  android.provider.Contacts.People;  
 
private  void  getColumnData(Cursor  cur){    
       if  (cur.moveToFirst())  {  
 
               String  name;    
               String  phoneNumber;    
               int  nameColumn  =  cur.getColumnIndex(People.NAME);    
               int  phoneColumn  =  cur.getColumnIndex(People.NUMBER);  
               String  imagePath;    
         
               do  {  
                       //  Get  the  field  values  
                       name  =  cur.getString(nameColumn);  
                       phoneNumber  =  cur.getString(phoneColumn);  
                       
                       //  Do  something  with  the  values.    
                       ...    
 
               }  while  (cur.moveToNext());  
 
       }  
}  

COMP 355 (Muppala) Data Storage 33


Content  Providers:  Modifying  Data  
•  Data  kept  by  a  content  provider  can  be  modified  by:  
–  Adding  new  records  
–  Adding  new  values  to  exis4ng  records  
–  Batch  upda4ng  exis4ng  records  
–  Dele4ng  records  
•  All  accomplished  using  ContentResolver  methods  
•  Use  ContentValues()  to  add  or  update  data  

COMP 355 (Muppala) Data Storage 34


Content  Providers:  Adding  Data  
•  Adding  new  records:  
import  android.provider.Contacts.People;  
import  android.content.ContentResolver;  
import  android.content.ContentValues;    
 
ContentValues  values  =  new  ContentValues();  
 
//  Add  Abraham  Lincoln  to  contacts  and  make  him  a  favorite.  
values.put(People.NAME,  "Abraham  Lincoln");  
//  1  =  the  new  contact  is  added  to  favorites  
//  0  =  the  new  contact  is  not  added  to  favorites  
values.put(People.STARRED,  1);  
 
Uri  uri  =  getContentResolver().insert(People.CONTENT_URI,  values);  

COMP 355 (Muppala) Data Storage 35


Content  Providers:  Adding  Data  
•  Adding  new  values:  
Uri  phoneUri  =  null;  
Uri  emailUri  =  null;  
 
phoneUri  =  Uri.withAppendedPath(uri,  People.Phones.CONTENT_DIRECTORY);  
 
values.clear();  
values.put(People.Phones.TYPE,  People.Phones.TYPE_MOBILE);  
values.put(People.Phones.NUMBER,  "1233214567");  
getContentResolver().insert(phoneUri,  values);  
 
//  Now  add  an  email  address  in  the  same  way.  
emailUri  =  Uri.withAppendedPath(uri,  People.ContactMethods.CONTENT_DIRECTORY);  
 
values.clear();  
//  ContactMethods.KIND  is  used  to  dis4nguish  different  kinds  of  
//  contact  methods,  such  as  email,  IM,  etc.    
values.put(People.ContactMethods.KIND,  Contacts.KIND_EMAIL);  
values.put(People.ContactMethods.DATA,  "test@example.com");  
values.put(People.ContactMethods.TYPE,  People.ContactMethods.TYPE_HOME);  
getContentResolver().insert(emailUri,  values);        

COMP 355 (Muppala) Data Storage 36


Content  Providers  
•  Use  ContentResolver.update()  to  batch  update  fields  
•  Use  ContentResolver.delete()  to  delete:  
–  A  specific  row  
–  Mul4ple  rows,  by  calling  the  method  with  the  URI  of  the  type  of  
record  to  delete  and  an  SQL  WHERE  clause  defining  which  rows  to  
delete  

COMP 355 (Muppala) Data Storage 37


Crea4ng  Content  Providers  
•  To  create  a  content  provider,  you  must:  
–  Set  up  a  system  for  storing  the  data.  Most  content  providers  store  
their  data  using  Android's  file  storage  methods  or  SQLite  databases,  
but  you  can  store  your  data  any  way  you  want.  
–  Extend  the  ContentProvider  class  to  provide  access  to  your  data  
–  Declare  the  content  provider  in  the  manifest  file  for  your  applica4on  
(AndroidManifest.xml)  

COMP 355 (Muppala) Data Storage 38


Crea4ng  Content  Providers  
•  Extending  the  ContentProvider  class  will  require:  
–  Implemen4ng  the  following  methods:  query(),  insert(),  update(),  
delete(),  getType(),  onCreate()  
–  Make  sure  that  these  implementa4ons  are  thread-­‐safe  as  they  may  be  
called  from  several  ContentResolver  objects  in  several  different  
processes  and  threads  
•  In  addi4on,  you  need  to:  
–  Declare  a  public  sta4c  final  URI  named  CONTENT_URI  
–  Define  the  column  names  that  the  content  provider  will  return  to  
clients  
–  Carefully  document  the  data  type  of  each  column  

COMP 355 (Muppala) Data Storage 39


Crea4ng  Content  Providers  
•  You  need  to  declare  the  content  provider  in  the  Manifest  file:  
•  Example:  
<provider  android:name="com.example.autos.AutoInfoProvider"  
                   android:authori4es="com.example.autos.autoinfoprovider"    
                   .  .  .  />  
</provider>  

COMP 355 (Muppala) Data Storage 40

You might also like