More Related Content
Similar to Android - Day 2 (20)
More from People Strategists (20)
Android - Day 2
- 1. Slide 1 of 19© People Strategists www.peoplestrategists.com
Data Storage in Android
- 2. Slide 2 of 19© People Strategists www.peoplestrategists.com Slide 2 of 57© People Strategists www.peoplestrategists.com
Data Storage in Android
Preferences
Files
SQLite
Network
- 3. Slide 3 of 19© People Strategists www.peoplestrategists.com Slide 3 of 57© People Strategists www.peoplestrategists.com
- 4. Slide 4 of 19© People Strategists www.peoplestrategists.com
Preference
A way of data persistence in Android that stores data in form of
key value pair like user specific setting, endpoint url, the
security access token etc.
Android stores shared preference settings
as XML file under:-
data/data/packagename/shared_prefs/
packagename_preferences.xml.
- 5. Slide 5 of 19© People Strategists www.peoplestrategists.com
Preference
once you uninstall the
application
once you clear
application data (through
Settings)
The data is lost when you perform any one following task:-
- 6. Slide 6 of 19© People Strategists www.peoplestrategists.com
Commonly used Shared Preferences
ListPreference
CheckBoxPreference
EditTextPreference
- 7. Slide 7 of 19© People Strategists www.peoplestrategists.com
Preference
Creating preferences:-
The Preferences Activity screen is defined in the preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key=“pref_screen“….>
<CheckBoxPreference
android:key=“checkbox_pref“…. />
<ListPreference
android:key=“list_pref“…./>
<EditTextPreference
android:key=“edit_pref”…./>
</PreferenceScreen>
- 8. Slide 8 of 19© People Strategists www.peoplestrategists.com
Create the Preferences Activity
public class AppPreferences extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref);
}
}
Preference
- 9. Slide 9 of 19© People Strategists www.peoplestrategists.com
Preference
To read the preferences values
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String strUserName = sp.getString("username", “ABC");
boolean bAppUpdates = sp.getBoolean("applicationUpdates",false);
String downloadType = sp.getString("downloadType","1");
- 10. Slide 10 of 19© People Strategists www.peoplestrategists.com Slide 10 of 57© People Strategists www.peoplestrategists.com
- 11. Slide 11 of 19© People Strategists www.peoplestrategists.com
Internal storage
It is private to your application and other applications
cannot access them (nor can the user)
When the app uninstalls, these files are removed.
External storage
External storage such as SD card can also store application data.
There's no security enforced upon files you save to the external
storage.
All applications can read and write files placed on the external
storage and the user can remove them for this they need Read
and Write permission.
Internal & External
Storage
- 12. Slide 12 of 19© People Strategists www.peoplestrategists.com
SQLite DB
SQLite is an Open Source database which supports standard
relational database features like SQL syntax, transaction etc.
It is embedded into every Android
device.
Database is by default saved in the
directory
DATA/data/APP_NAME/databases/FILENA
ME.
- 13. Slide 13 of 19© People Strategists www.peoplestrategists.com
SQLite DB
Necessary classes for working with databases:-
SQLiteHelper
SQLiteDataBase
Cursor
- 14. Slide 14 of 19© People Strategists www.peoplestrategists.com
Content Provider
- 15. Slide 15 of 19© People Strategists www.peoplestrategists.com Slide 15 of 57© People Strategists www.peoplestrategists.com
Content Provider
An Android component that supplies data from one app to another on
request.
A content provider can use different ways to store its data and the data can be
stored in a database, infiles, or even over a network.
- 16. Slide 16 of 19© People Strategists www.peoplestrategists.com Slide 16 of 57© People Strategists www.peoplestrategists.com
Content Provider
- 17. Slide 17 of 19© People Strategists www.peoplestrategists.com Slide 17 of 57© People Strategists www.peoplestrategists.com
Content Provider
Creating content provider
Create a content provider class that extends the contentproviderbaseclass.
Define your content provider uri address.
Create your own database to keep the content.
Implement content provider queries.
Register your content provider in menifest file.
- 18. Slide 18 of 19© People Strategists www.peoplestrategists.com Slide 18 of 57© People Strategists www.peoplestrategists.com
Demo
Security and Packages
- 19. Slide 19 of 19© People Strategists www.peoplestrategists.com Slide 19 of 57© People Strategists www.peoplestrategists.com
Demo
Talking to the Server